import requests
from lxml import etree
url = 'https://movie.douban.com/subject/1292052/'
data = requests.get(url).text
s=etree.HTML(data)
film=s.xpath('//*[@id="content"]/h1/span[1]/text()')
print('film_title:',film)
但是我得到了一个类似这样的错误:
Traceback (most recent call last):
File "<ipython-input-5-4e3f3aa89a1c>", line 8, in <module>
film=s.xpath('//*[@id="content"]/h1/span[1]/text()')
AttributeError: 'NoneType' object has no attribute 'xpath'
我不知道为什么会发生这种情况,也不知道如何纠正它。
发布于 2020-12-31 00:51:38
您可以添加标头来模拟浏览器
headers ={‘用户代理’:‘Mozilla/5.0 (Windows NT6.1;WOW64) AppleWebKit/535.1 (KHTML,如壁虎)Chrome/14.0.835.163Safari/535.1’}
url = 'https://movie.douban.com/subject/1292052/‘
data = requests.get(url,headers = headers).text
https://stackoverflow.com/questions/60275099
复制相似问题