from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
myUrl = "https://mee6.xyz/levels/159962941502783488"
uClient = uReq(myUrl)
pageHtml = uClient.read()
print("pageHtml)
我正在尝试访问一个页面以开始抓取它,但它回复说HTTP是被禁止的,我查找了其他结果,但它们与我正在执行代码的方式不匹配
答案 0 :(得分:0)
您因用户代理而被阻止。尝试欺骗您的用户代理,如下所示:
from urllib.request import urlopen as uReq
from urllib.request import Request
from bs4 import BeautifulSoup as soup
myUrl = "https://mee6.xyz/levels/159962941502783488"
req = Request(
myUrl,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
)
uClient = uReq(req)
pageHtml = uClient.read()
print(pageHtml)