出錯內容1:
Traceback (most recent call last): File "E:\python_workplace\Python實驗\實驗四\test4_2\豆瓣評分.py", line 104, in <module> main(bookId, 60, 3, 'hot') File "E:\python_workplace\Python實驗\實驗四\test4_2\豆瓣評分.py", line 64, in main soup = BeautifulSoup(html, 'html.parser') File "E:\python_workplace\venv\lib\site-packages\bs4\__init__.py", line 275, in __init__ elif len(markup) <= 256 and ( TypeError: object of type 'NoneType' has no len()
出錯原因:由於爬取的是豆瓣的網站,豆瓣具有反爬取機制,所以這需要偽裝成瀏覽器
修改方式:
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"} html = requests.get(url=url,headers=headers) soup = BeautifulSoup(html, 'lxml')
接下來之前的錯誤消失,出現下面的錯誤
出錯內容2:
Traceback (most recent call last): File "E:\python_workplace\Python實驗\實驗四\test4_2\豆瓣評分.py", line 102, in <module> main(bookId, 60, 3, 'hot') File "E:\python_workplace\Python實驗\實驗四\test4_2\豆瓣評分.py", line 62, in main soup = BeautifulSoup(html, 'lxml') File "E:\python_workplace\venv\lib\site-packages\bs4\__init__.py", line 275, in __init__ elif len(markup) <= 256 and ( TypeError: object of type 'Response' has no len()
出錯位置:
修改為:
soup = BeautifulSoup(html, 'lxml')
修改后結果: