我試圖刮掉黃頁的數據,但我跑到哪裏我無法得到每個企業名稱和地址/電話的文本。我正在使用下面的代碼,我哪裏錯了?我試圖打印每個企業的文本,但只是打印出來,目的是爲了看到它,因爲我測試,但一旦我完成後,我會將數據保存到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抱歉。我剛剛取消了它,我即將進行測試。 :) –