簡體   English   中英

無法使用請求模塊從網站上抓取啤酒廠的名稱

[英]Can't scrape breweries' names from a website using the requests module

我創建了一個腳本來使用請求模塊從該網站收集啤酒廠的名稱,但是當我執行該腳本時,它最終什么也得不到。 我在頁面源代碼中以及通常通過開發工具找到的任何未記錄的 API 中查找標題,但沒有找到。

import requests
from bs4 import BeautifulSoup

link = "https://www.brewersassociation.org/directories/breweries/"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}

res = requests.get(link,headers=headers)
soup = BeautifulSoup(res.text,"html.parser")
for item in soup.select(".company-content > h3[itemprop='name']"):
    print(item.text)

你可以試試:

import requests
import pandas as pd


url = 'https://www.brewersassociation.org/wp-content/themes/ba2019/json-store/breweries/breweries.json'

data = requests.get(url).json()

df = pd.DataFrame(data)
df = pd.concat([df, df.pop('BillingAddress').apply(pd.Series, dtype=object)], axis=1)
df.pop('attributes')

# print sample data, total length should be 26802 breweries:
print(df.head().to_markdown(index=False))

印刷:

ID 名稱 家長 電話 網站 Brewery_Type__c Is_Craft_Brewery__c Voting_Member__c Membership_Record_Item__c Membership_Record_Paid_Through_Date__c Membership_Record_Status__c Account_Badges__c 城市 國家 國家代碼 地理編碼准確性 緯度 經度 郵政編碼 state 州代碼 街道
0014x000012jyoHAAQ 規划中的啤酒廠 - 蒙特雷 (811) 244-8078 規划中的啤酒廠 錯誤的 錯誤的 蒙特雷 墨西哥 MX 堵塞 25.6444 -100.275 64850 圖坎 362
0014x000012jyoJAAQ Sekinoichi-shuzo Co.,Ltd/Iwai Brewery +81-191-21-1144 www.sekinoichi.co.jp 酒館 錯誤的 錯誤的 一關市 日本 J.P 地址 38.9314 141.132 021-0885 5-42 田村町
0014x000012jyoKAAQ 塞爾比(米德爾伯勒)啤酒廠有限公司 01757 702826 錯誤的 錯誤的 塞爾比 英國 國標 堵塞 53.7871 -1.07141 YO8 3LL 131米爾蓋特
0014x000012jyoLAAQ 森德羅釀酒公司 www.senderobrewing.com 規划中的啤酒廠 錯誤的 錯誤的 啤酒廠會員資格 2019-10-31 已到期 聖佩德羅蘇拉 洪都拉斯 氫氮 城市 15.5039 -88.0157 21102 洛斯阿爾卑斯,麥凱大道
0014x000012jyoMAAQ Ser Bhum 小型釀酒廠 錯誤的 錯誤的 啤酒廠會員資格 2017-08-31 已到期 廷布 不丹 英國電信 洪措洪措

暫無
暫無

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

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