如果在抓取时使用Python中的语句

如果在抓取时使用Python中的语句,python,selenium,if-statement,web-scraping,itertools,Python,Selenium,If Statement,Web Scraping,Itertools,我想在那里添加if语句: 如果未找到网站链接和电话,则写入N,N 如果未找到网站链接,则键入此脚本。如果未找到电话,则键入 我还要补充一点: 如果未找到网站和电话,请键入N,N 这是我的密码: from selenium import webdriver import csv import pandas import itertools with open("sans.csv",'r') as s: s.read() driver = webdriver.Firefox() ur

我想在那里添加
if语句

如果未找到网站链接和电话,则写入N,N

如果未找到网站链接,则键入此脚本。如果未找到电话,则键入

我还要补充一点:

如果未找到网站和电话,请键入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')

driver.close()

print("Done")

谢谢

您可以将
else
添加到
for
循环中。当
website\u link
phone
为空时,它将运行,以防循环不运行

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')

有关
For-else
结构的更多信息,请参阅。

您可以将
else
添加到
For
循环中。当
website\u link
phone
为空时,它将运行,以防循环不运行

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')

有关
For-else
结构的更多信息,请参见。

可能是他希望迭代器在
网站链接和
电话中迭代并检查

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')
         if(website_link == None) and (Phone == None):
               s.write('N,N')



可能他希望迭代器在
网站链接和
电话中迭代并检查
None

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')
         if(website_link == None) and (Phone == None):
               s.write('N,N')



正是我会用的方法。你能简单解释一下,
是如何。。。否则…
子句在python中工作,因为询问者似乎是从python开始的?@adrio这是一个很好的例子,我知道
的作用是什么。。。否则…
子句会,我建议将该信息添加到答案中。这个链接实际上很好,我会把它包括在你的答案中。@guy这是在下一行输入n,如果有什么问题的话found@HamzaLachi只需添加新行字符
s.write('N,N\N')
,这正是我使用的方式。你能简单解释一下,
是如何。。。否则…
子句在python中工作,因为询问者似乎是从python开始的?@adrio这是一个很好的例子,我知道
的作用是什么。。。否则…
子句会,我建议将该信息添加到答案中。这个链接实际上很好,我会把它包括在你的答案中。@guy这是在下一行输入n,如果有什么问题的话found@HamzaLachi只需添加新行字符
s.write('N,N\N')
如果
website\u link
phone
,其中
itertools.zip\u longest
将引发异常。而且它们在任何情况下都不会是
None
。@伙计,你是真的是的,在这种情况下,我们可以简单地用“izip_longest”或“zip”代替“zip_longest”。请告诉我它是否有效。如果
网站链接
手机
其中
itertools.zip\u最长
将引发异常。而且它们在任何情况下都不会是
None
。@伙计,你是真的是的,在这种情况下,我们可以简单地用“izip_longest”或“zip”代替“zip_longest”。只要让我知道它是否有效。