簡體   English   中英

BeautifulSoup web 抓取點擊按鈕后獲取信息的網頁

[英]BeautifulSoup web scraping for a webpage where information is obtained after clicking a button

所以,我正在嘗試獲取一些餐館的 Yelp 頁面的“設施和更多”部分。 問題是我可以從首先顯示的餐廳的 Yelp 頁面訪問設施。 但是,它具有“n more”按鈕,單擊該按鈕可提供更多便利。 Using BeautifulSoup and selenium with the webpage url and using BeautifulSoup with requests gives exact same results and I am stuck as to how to open the whole Amenities before grabbing them in my code. 下面的兩張圖片顯示了單擊按鈕之前和之后發生的情況。 在此處輸入圖像描述

在此處輸入圖像描述

  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 個元素。 我只展示與我的問題相關的內容:

在此處輸入圖像描述

另一種方法是直接從站點上的 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 工作,但大多數時候不需要這樣做,因為它只是發出請求並格式化數據以供顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM