python3爬取网页,报错AttributeError: 'NoneType' object has no attribute 'xpath'?

#导入所需要的包import sysimport codecssys.stdout = codecs.getwriter("utf-8")(sys.stdout)import requestsfrom lxml import etreeurl = '
https://movie.douban.com/subject/1292052/
'data = requests.get(url).texts=etree.HTML(data)#获取电影名称、导演、主演、时长信息film=s.xpath('//*[@id="content"]/h1/span[1]/text()')director=s.xpath('//*[@id="info"]/span[1]/span[2]/a/text()')actor=s.xpath('//*[@id="info"]/span[3]/span[2]/a/text()')time=s.xpath('//*[@id="info"]/span[13]/text()')#显示获取的信息内容print('name:',film)print('director:',director)print('actors:',actor)print('duration:',time)求指点!!!
最新回答
昔年°c

2024-06-05 20:50:37

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'}
data = requests.get(url,headers=headers).text
加一个头部就行了
余盼兮

2024-06-05 03:33:21

“NoneType”是找不到标签造成的。
有可能是你写的不对
还有可能是你没更改访问头被网站封ip了