簡體   English   中英

Web 使用 Python 和美麗湯進行抓取:錯誤“'page' is not defined”

[英]Web scraping using Python and Beautiful soup: error “'page' is not defined”

從一個投注網站,我想收集投注率。 檢查頁面后,我注意到這些費率包含在eventprice class 中。 按照這里的解釋,我因此使用 Beautifulsoup 模塊在 Python 中編寫了此代碼:

from bs4 import BeautifulSoup
import urllib.request
import re

url = "http://sports.williamhill.com/bet/fr-fr"

try:
    page = urllib.request.urlopen(url)
except:
    print("An error occured.")

soup = BeautifulSoup(page, 'html.parser')

regex = re.compile('eventprice')
content_lis = soup.find_all('button', attrs={'class': regex})
print(content_lis)

但是,我收到以下錯誤:

“(...) 第 12 行,在 soup = BeautifulSoup(page, 'html.parser') NameError: name 'page' is not defined”

如果您打印異常詳細信息,您將看到發生了什么:

try:
    page = urllib.request.urlopen(url)
except Exception as e:
    print(f"An error occurred: {e}")

Output

An error occurred: HTTP Error 403: Forbidden
Traceback (most recent call last):
  File ".../main.py", line 12, in <module>
    soup = BeautifulSoup(page, 'html.parser')
NameError: name 'page' is not defined

urlopen() 引發異常,導致未定義的“頁面”變量。 在這種情況下,它是 403,這意味着您可能需要添加身份驗證才能訪問此 URL。

更新:

403 響應意味着無法以您嘗試訪問它的方式訪問此 URL。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403

暫無
暫無

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

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