【发布时间】:2020-02-07 12:52:12
【问题描述】:
我正在尝试使用 python 和 beautifulsoup 从 yelp 获取潜在客户,但我无法捕获电话名称地址和 wesbite 字段(可选)。 我收到以下错误,这是我尝试搜索并找到不同解决方案的代码,但它们对我不起作用。
这是我的代码
from bs4 import BeautifulSoup
import requests
import sys
import csv
import requests, re, json
## Get the min and max page numbers
pagenum=0
maxpage =0
## loop go thourgh the pages
while pagenum <= maxpage:
newsu =pagenum
newsu = str(newsu)
csvname = 'cardealers'+newsu+'.csv';
csvfile = open(csvname , 'w',encoding="utf-8")
csv_writer = csv.writer(csvfile)
csv_writer.writerow(['Business name', 'phone' , 'address'] )
headers = {'User-Agent':'Mozilla/5.0'}
r = requests.get('https://www.yelp.com/search?find_desc=Used%20Car%20Dealers&find_loc=New%20York%2C%20NY&ns=1&sortby=review_count&start={}'.format(pagenum), headers = headers)
p = re.compile(r'PRELOADED_STATE__ = (.*?);')
data = json.loads(p)
print(data)
pagenum =pagenum+1
for item in data['searchResult']['results']:
name = item['businessName']
phone=item['phone']
address= ([item['address'],item['city'], item['state'], item['postalcode']])
csv_writer.writerow([name, phone , address ])
print(name)
csvfile.close()
这是错误信息。
Traceback(最近一次调用最后一次):文件 “\Python\Python36\scraper\scrape.py”,第 22 行,在 data = json.loads(p) 文件“\Python\Python36\lib\json__init__.py”,第 348 行,加载中 'not {!r}'.format(s.class.name)) TypeError: JSON object must be str, bytes or bytearray, not 'SRE_Pattern'
【问题讨论】:
-
指定问题什么不起作用?您正在使用 SRE_Pattern 加载 JSON。
-
我尝试获取包含电话地址和姓名的元素类,但所有类都相同,而且我是新手,所以我不知道如何以其他方式进行操作看到关于 json 响应,所以我尝试在 json 上创建它,但现在我收到了我之前提到的错误
-
你想要什么?
-
我想从 yelp 中获取线索,例如公司名称地址和电话号码?我只是想弄清楚我做错了什么
-
你必须使用 BeautifulSoup 来获取请求 URL 的数据。
标签: python web-scraping beautifulsoup