python+requests+xpath爬取贴吧评论总是报错NoneType

c491465346 2018-01-08 10:52:44
本人新人,刚学爬虫。看了个例子仿照着写了个爬取贴吧回复的,但是运行总是报错无类型......我估计是我的xpath定位问题,但我不知道该怎么改,或者是其他地方错了。哪位大神帮忙指导下!!谢谢!!以下是代码
import requests
from lxml import etree
import json
import time
from requests.exceptions import RequestException
from pymongo import MongoClient

client=MongoClient()
db=client.tieba #连接tieba数据库,没有则自动创建
my_set=db.comment #使用comment集合,没有则自动创建

def get_html(url):
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return None
except RequestException:
return None

def parse_html(html):
selector=etree.HTML(html)
data=selector.xpath('//div[@class="l_post l_post_bright j_l_post clearfix "]')
for each in data:
rs=each.xpath('@data-field')[0]
rs=json.loads(rs)
author=rs.get('author').get('user_name')
author_id=rs.get('content').get('post_id')
content=each.xpath('div/div/cc/div[@id="post_content_%s"]/text()'% author_id)[0].strip()
date=rs.get('content').get('date')
yield {
'author':author,
'content':content,
'date':date
}

def save_to_txt(result):
print('正在存储:',result)
with open('tieba.txt','a',encoding='utf-8') as f:
f.write('回帖作者:'+result['author']+'\n')
f.write('回帖内容:' + result['content'] + '\n')
f.write('回帖时间:' + result['date'] + '\n')
f.write('\n')

def main(url):
html=get_html(url)
if html:
for result in parse_html(html):
save_to_txt(result)

if __name__=='__main__':
for i in range(1,21):
url = 'https://tieba.baidu.com/p/5501399200?fid=59099&pn='+str(i)
print('正在爬取第%s页'% str(i))
main(url)
time.sleep(10)

爬取的网址是https://tieba.baidu.com/p/5501399200?fid=59099&pn=1
...全文
1140 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
苏酒酒 2018-01-18
  • 打赏
  • 举报
回复
1、写爬虫的时候最好带上HEADERS
2、如果id都不一样,则不建议用id,最好用class或其他样式都行
3、但凡id里带了数字,且都不同,这说明可能用了json,你求本页面是没有用的

这才是你要的
m0_37800356 2018-01-10
  • 打赏
  • 举报
回复
Perse_html没有返回值,自然是NoneType 你对他遍历自然报错
  • 打赏
  • 举报
回复
报错信息贴出来看看吧
c491465346 2018-01-09
  • 打赏
  • 举报
回复
引用 1 楼 oyljerry的回复:
先看你拿到的内容,再看xpath定位是否正确。
直接报错,没有内容返回
oyljerry 2018-01-08
  • 打赏
  • 举报
回复
先看你拿到的内容,再看xpath定位是否正确。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧