BeautifulSoup web scraping用于在单击按钮后获取信息的网页

2024-06-07 01:45:26 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我试图在一些餐馆的Yelp页面上找到“便利设施和更多”部分。问题是,我可以从餐厅的yelp页面首先显示的设施。不过,它有“n more”按钮,单击该按钮可提供更多便利设施。在网页url中使用BeautifulSoup和selenium以及在请求中使用BeautifulSoup都会得到完全相同的结果,在我的代码中使用BeautifulSoup之前,我一直在思考如何打开整个便利设施。下面两张图片显示了单击按钮前后发生的情况。 enter image description here

enter image description here

  1. 单击“5个更多属性”之前:第一张图片显示了4个“div”,其中包含“span”,我可以使用上述任何一种方法获得它
  2. 单击“5个更多属性”后:第二张图片显示了9个“div”,其中包含我试图访问的“span”

下面是使用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')

结果:“跨度”中有990个元素。我只展示与我的问题相关的内容:

enter image description here


Tags: 代码importdivsource属性driverseleniumpage
1条回答
网友
1楼 · 发布于 2024-06-07 01:45:26

另一种方法是直接从站点上的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工作,但大多数情况下这并不需要,因为它只是发出请求并格式化数据以供显示

相关问题 更多 >