Python BeautifulSoup| HTTP错误:禁止
我被困在这里,它给了我httperror:第4行中禁止。当我尝试与其他网站,然后它是工作,但在这个网站上它不会工作,为什么Python BeautifulSoup| HTTP错误:禁止,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我被困在这里,它给了我httperror:第4行中禁止。当我尝试与其他网站,然后它是工作,但在这个网站上它不会工作,为什么 指定User-AgentHTTP头以从服务器获得正确的响应。例如: import urllib.request from urllib.request import urlopen from bs4 import BeautifulSoup as bs url = "https://socialblade.com/youtube/top/50" hea
指定
User-Agent
HTTP头以从服务器获得正确的响应。例如:
import urllib.request
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
url = "https://socialblade.com/youtube/top/50"
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'}
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
soup = bs(response.read(), 'html.parser')
print(soup.prettify())
印刷品:
<!DOCTYPE html>
<head>
<title>
Top 50 YouTubers sorted by SB Score - Socialblade YouTube Stats | YouTube Statistics
</title>
...
按SB分数排序的前50名YouTube-社交刀片YouTube统计数据| YouTube统计数据
...
请参见和
<!DOCTYPE html>
<head>
<title>
Top 50 YouTubers sorted by SB Score - Socialblade YouTube Stats | YouTube Statistics
</title>
...