Python 为什么它会跳过整个for循环?
我已经创建了一个网站刮刀,将刮黄页上的所有信息(用于教育目的) 代码是我上面的刮板部分。我已经在soup和for循环中创建了断点,甚至没有一行for循环被执行。没有抛出错误。我试着用同样的方法打印1-10的数字,效果很好,但不起作用。为什么 谢谢已经找到答案Python 为什么它会跳过整个for循环?,python,Python,我已经创建了一个网站刮刀,将刮黄页上的所有信息(用于教育目的) 代码是我上面的刮板部分。我已经在soup和for循环中创建了断点,甚至没有一行for循环被执行。没有抛出错误。我试着用同样的方法打印1-10的数字,效果很好,但不起作用。为什么 谢谢已经找到答案 我使用一个文本可视化工具来查找“r.content”中的内容。我对它进行了加密,得到了一个干净的HTML,检查了HTML文件,最后发现浏览器不受支持,所以我删除了请求头并运行了代码,最后得到了我想要的内容,可能是因为find\u all的结
我使用一个文本可视化工具来查找“r.content”中的内容。我对它进行了加密,得到了一个干净的HTML,检查了HTML文件,最后发现浏览器不受支持,所以我删除了请求头并运行了代码,最后得到了我想要的内容,可能是因为
find\u all
的结果是空的吧?你检查过了吗?因为你的迭代可能是空的。使用print()
查看变量中的内容。@abcdef首先print(soup.find_all(class=“business name”)
,因为运行for
循环最重要。除了子句之外,蝙蝠侠!
def actual_yellow_pages_scrape(link,no,dir,gui,sel,ypfind,terminal,user,password,port,type):
print(link,no,dir,gui,sel,ypfind,terminal,user,password,port,type)
r = requests.get(link,headers=REQUEST_HEADERS)
soup = BeautifulSoup(r.content,"html.parser")
workbook = xlwt.Workbook()
sheet = workbook.add_sheet(str(ypfind))
count = 0
for i in soup.find_all(class_="business-name"):
sheet.write(count,0,str(i.text))
sheet.write(count,1,str("http://www.yellowpages.com"+i.get("href")))
r1 = requests.get("http://www.yellowpages.com"+i.get("href"))
soup1 = BeautifulSoup(r1.content,"html.parser")
website = soup1.find("a",class_="custom-link")
try:
print("Acquiring Website")
sheet.write(count,2,str(website.get("href")))
except:
sheet.write(count,2,str("None"))
email = soup1.find("a",class_="email-business")
try:
print(email.get("href"))
EMAIL = re.sub("mailto:","",str(email.get("href")))
sheet.write(count,3,str(EMAIL))
except:
sheet.write(count,3,str("None"))
phonetemp = soup1.find("div",class_="contact")
try:
phone = phonetemp.find("p")
print(phone.text)
sheet.write(count,4,str(phone.text))
except:
sheet.write(count,4,str("None"))
reviews = soup1.find(class_="count")
try:
print(reviews.text)
sheet.write(count,5,str(reviews.text))
except:
sheet.write(count,5,str("None"))
count+=1
save = dir+"\\"+ypfind+str(no)+".xls"
workbook.save(save)
no+=1
for i in soup.find_all("a",class_="next ajax-page"):
print(i.get("href"))
actual_yellow_pages_scrape("http://www.yellowpages.com"+str(i.get("href")),no,dir,gui,sel,ypfind,terminal,user,password,port,type)