mardi 12 novembre 2019

How to fix If Statment in Python Whle Scrapping

This Else Statement is working Half! if website and phone not found it will type N,N but the issue is if website and phone is found it will also write in new line N,N I Don't Like This

I Need Something Like This: If Website And Phone Not Found Type N,N and if website and phone found add new line add the second URL details

ERROR:

This Script is typing N,N when website and phone found and when not found it will type detail and make new line and add N,N

from selenium import webdriver 
import csv
import pandas
import itertools

with open("sans.csv",'r') as s:
    s.read()

driver = webdriver.Firefox()

url = 'https://www.yelp.com/biz/konomama-san-francisco?osq=Restaurants'

driver.get(url)
website_link = driver.find_elements_by_css_selector('.text--offscreen__373c0__1SeFX+ .link-size--default__373c0__1skgq')

phone = driver.find_elements_by_css_selector('.text--offscreen__373c0__1SeFX+ .text-align--left__373c0__2pnx_')

items = len(website_link)
with open("sans.csv", 'a', encoding="utf-8") as s:
    for combination in itertools.zip_longest(website_link, phone):
        s.write(f'{combination[0].text if combination[0] else "N"}, {combination[1].text if combination[1] else "N"}\n')
    else:
        s.write('N, N')

driver.close()

print("Done")

Aucun commentaire:

Enregistrer un commentaire