我正在尝试从Metacritic中刮取电影评分。这是代码中引发错误的部分。
text = text.replace("_","-")
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent,}
URL = "http://metacritic.com/" + text
request=urllib.request.Request(URL,None,headers)
try:
response = urllib.request.urlopen(request)
data = response.read()
soup = BeautifulSoup(data,'html.parser')
metacritic_rating = "Metascore: " + soup.find("span",class_="metascore_w").get_text()
send_message(metacritic_rating,chat)
except:
pass
我修改了使用以下内容编写的内容:https://stackoverflow.com/a/42441391/8618880
由于以下原因,我无法使用requests.get()
:urllib2.HTTPError: HTTP Error 403: Forbidden
我正在寻找一种获取页面状态代码的方法。当我使用requests.get()
时,我能够找到一种方法。
我签出了所有标题为urllib.error.HTTPError: HTTP Error 404: Not Found Python
的答案,但找不到任何帮助。
感谢您的帮助。
答案 0 :(得分:1)
我想这就是你想要的:
import urllib
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent,}
URL = "http://metacritic.com/" + text
request=urllib.request.Request(URL,None,headers)
try:
response = urllib.request.urlopen(request)
data = response.read()
soup = BeautifulSoup(data,'html.parser')
metacritic_rating = "Metascore: " + soup.find("span",class_="metascore_w").get_text()
send_message(metacritic_rating,chat)
except urllib.error.HTTPError as err:
#print(err.code)
if err.code == 403:
<do something>
else:
pass
输出:
403