如果在抓取时使用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”。只要让我知道它是否有效。