我正在使用request模块查看单词列表中的项目是否是https://www.britannica.com上的文章。我当前的代码是:
import requests
words = ['no', 'yes', 'thermodynamics', 'london', 'Max-Factor', 'be']
for word in words:
request = requests.head('https://www.britannica.com/topic/' + word.lower())
if request.status_code == 200:
print(">EXISTS")
print('https://www.britannica.com/topic/' + word.lower())
print("<")
else:
print(">DOESNT EXIST")
print('https://www.britannica.com/topic/' + word.lower())
print("<")“‘Be”是唯一打印“EXIST”的字符串,但是“热力学”、“london”和“Max-Factor”也存在,并且程序打印“不存在”。如果我单独对热力学进行操作,它会正确地打印“EXISTS”。造成这种差异的原因和可能的解决方法是什么?可能是不同网页的加载时间('Be‘最小)?
发布于 2017-06-20 02:21:04
显然,britanica.com使用重定向,可能是为了负载平衡,所以你通常会得到301而不是200的状态。如果您使用以下命令,requests模块可以执行重定向:
request = requests.head('https://www.britannica.com/topic/' + word.lower(),
allow_redirects=True)https://stackoverflow.com/questions/44637223
复制相似问题