我试图刮掉黄页的数据,但我跑到哪里我无法得到每个企业名称和地址/电话的文本。我正在使用下面的代码,我哪里错了?我试图打印每个企业的文本,但只是打印出来,目的是为了看到它,因为我测试,但一旦我完成后,我会将数据保存到csv。Python 3刮黄页
import csv
import requests
from bs4 import BeautifulSoup
#dont worry about opening this file
"""with open('cities_louisiana.csv','r') as cities:
lines = cities.read().splitlines()
cities.close()"""
for city in lines:
print(city)
url = "http://www.yellowpages.com/search? search_terms=businesses&geo_location_terms=amite+LA&page="+str(count)
for city in lines:
for x in range (0, 50):
print("http://www.yellowpages.com/search?search_terms=businesses&geo_location_terms=amite+LA&page="+str(x))
page = requests.get("http://www.yellowpages.com/search?search_terms=businesses&geo_location_terms=amite+LA&page="+str(x))
soup = BeautifulSoup(page.text, "html.parser")
name = soup.find_all("div", {"class": "v-card"})
for name in name:
try:
print(name.contents[0]).find_all(class_="business-name").text
#print(name.contents[1].text)
except:
pass
请勿使用'except:pass',因为您可能有错误而您不知道它。至少使用'except Exception as e:print(e)' – furas
你是对的,因为我的代码确实有错误,我抛出了这个try,除了绕过它。 –
@alecxe抱歉。我刚刚取消了它,我即将进行测试。 :) –