【发布时间】:2022-01-20 18:23:51
【问题描述】:
import requests as requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
'cache-control': 'private, max-age=0, no-cache'
}
htmlFile=requests.get('https://www.goodreads.com/shelf/show/1', headers=headers).text
sou=BeautifulSoup(htmlFile,"html.parser")
re = sou.find(class_="leftContainer").findAll(class_="bookTitle")
print(re)
内容不是动态的,不需要JS之类的。
那么为什么有时 None 对象会返回呢?
反复尝试的while循环可以解决问题,但这并不是真正的解决方案。
【问题讨论】:
-
我实际上无法在我的机器上重现此错误,可能运行了大约 50 次。有时有多罕见?
-
大约170次后才收到,可能是服务器限制了请求数?
-
我在大约第 100 个请求时得到了它。响应代码是 504,这意味着这是服务器的问题,这可能意味着您同时发出了太多请求
-
谢谢,你是怎么得到响应码的?美汤里有支持这个的功能吗?
-
好吧,你必须先了解请求。
requests.get方法返回具有status_code字段的Response对象。在直接使用text之前,您至少必须打印它
标签: python beautifulsoup python-requests