因此,我正在尝试为几家餐厅获取 Yelp 页面的“便利设施和更多”部分。问题是我可以从最先显示的餐厅 yelp 页面访问 Amenities。然而,它有“n more”按钮,点击后会提供更多便利。将 BeautifulSoup 和 selenium 与网页 url 一起使用,并将 BeautifulSoup 与请求一起使用会给出完全相同的结果,我一直不知道如何在我的代码中获取它们之前打开整个 Amenities。下面的两张图片显示了单击按钮之前和之后发生的情况。
这里是使用 selenium/beautifulsoup 的代码
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
URL ='https://www.yelp.com/biz/ziggis-coffee-longmont'
driver =
webdriver.Chrome(r"C:\Users\Fariha\AppData\Local\Programs\chromedriver_win32\chromedriver.exe")
driver.get(URL)
yelp_page_source_page1 = driver.page_source
soup = BeautifulSoup(yelp_page_source_page1,'html.parser')
spans = soup.find_all('span')
结果:“spans”中有 990 个元素。我只展示与我的问题相关的内容:
答案 0 :(得分:2)
另一种方法是直接从站点上的 JSON api 中提取数据。这可以在没有 selenium 开销的情况下完成,如下所示:
from bs4 import BeautifulSoup
import requests
import json
session = requests.Session()
r = session.get('https://www.yelp.com/biz/ziggis-coffee-longmont')
#r = session.get('https://www.yelp.com/biz/menchies-frozen-yogurt-lafayette')
soup = BeautifulSoup(r.content, 'lxml')
# Locate the business ID to use (from JSON inside one of the script entries)
for script in soup.find_all('script', attrs={"type" : "application/json"}):
gaConfig = json.loads(script.text.strip('<!-->'))
try:
biz_id = gaConfig['gaConfig']['dimensions']['www']['business_id'][1]
break
except KeyError:
pass
# Build a suitable JSON request for the required information
json_post = [
{
"operationName": "GetBusinessAttributes",
"variables": {
"BizEncId": biz_id
},
"extensions": {
"documentId": "35e0950cee1029aa00eef5180adb55af33a0217c64f379d778083eb4d1c805e7"
}
},
{
"operationName": "GetBizPageProperties",
"variables": {
"BizEncId": biz_id
},
"extensions": {
"documentId": "f06d155f02e55e7aadb01d6469e34d4bad301f14b6e0eba92a31e635694ebc21"
}
},
]
r = session.post('https://www.yelp.com/gql/batch', json=json_post)
j = r.json()
business = j[0]['data']['business']
print(business['name'], '\n')
for property in j[1]['data']['business']['organizedProperties'][0]['properties']:
print(f'{"Yes" if property["isActive"] else "No":5} {property["displayText"]}')
这将为您提供以下条目:
Ziggi's Coffee
Yes Offers Delivery
Yes Offers Takeout
Yes Accepts Credit Cards
Yes Private Lot Parking
Yes Bike Parking
Yes Drive-Thru
No No Outdoor Seating
No No Wi-Fi
这是怎么解决的?
你最好的朋友是浏览器的网络开发工具。有了这个,您可以查看为获取信息而提出的请求。正常的流程是下载初始 HTML 页面,这会运行 javascript 并请求更多数据以进一步填充页面。
诀窍是首先找到您想要的数据的位置(通常以 JSON 形式返回),然后确定您需要重新创建发出请求所需的参数。
要进一步理解此代码,请使用 print()
。打印所有内容,它将向您展示每个部分如何建立在下一个部分上。脚本就是这样写的,一点一点。
使用 Selenium 的方法允许 javascript 工作,但大多数时候这不是必需的,因为它只是发出请求并格式化数据以供显示。