python爬取有关熊安新区的网易评论
来源:互联网 发布:js date获取年月日 编辑:程序博客网 时间:2024/04/27 22:32
从收到任务到完成任务大概用了两天半,期间出现好多问题,如:
1.爬到的信息格式不正确,不是字典形式;
2.爬出来的缺少信息;
3.爬出来的信息无法用中文显示;
4.爬出来的信息无法写进文件
.......
废话不多说,还是直接上代码吧.....不懂的自行百度~~~
# -*- coding:utf-8 -*-import reimport urllib2import jsonimport timeimport sysreload(sys)sys.setdefaultencoding('utf8')def get_dict(): data = [] f = open('xa35.json', 'a+') for k in range(0, 4): if k == 0: url1 = 'http://comment.news.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/CHEN8TPF0001875P/comments/hotList?offset=0&limit=40&showLevelThreshold=72&headLimit=1&tailLimit=2&callback=getData&ibc=newspc' user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0" headers = {'User-Agent': user_agent} response = urllib2.Request(url1, headers=headers) data = urllib2.urlopen(response).read() else: url2 = 'http://comment.news.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/CHEN8TPF0001875P/comments/newList?offset=%d&limit=30&showLevelThreshold=72&headLimit=1&tailLimit=2&callback=getData&ibc=newspc&_=1492685047382' % k user_agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0" headers = {'User-Agent': user_agent} response = urllib2.Request(url2, headers=headers) data = urllib2.urlopen(response).read() reg = re.compile(r'getData\(') data1 = reg.sub('', data) reg1 = re.compile(r"\)\;") data = reg1.sub(' ', data1) data = json.loads(data) for i in data['comments']: dict = {} nc = data['comments'][i]['user'].setdefault('nickname', '""') dict['nick'] = nc pl = data['comments'][i]['content'] dict['comment'] = pl dz = data['comments'][i]['user'].setdefault('location', '""') dict['region'] = dz userId = str(data['comments'][i]['user'].setdefault('userId', '""')) dict['userid'] = userId commentId = str(data['comments'][i]['commentId']) dict['id'] = commentId dict1 = json.dumps(dict,ensure_ascii=False) print dict1 f.write(dict1+"\n") f.close()rs = get_dict()
顺带附一张结果图:
0 0
- python爬取有关熊安新区的网易评论
- python爬取网易评论
- Python爬取网易新闻动态评论
- python爬取网易动态评论
- python 爬取网易新闻评论
- python关于用BeautifulSoup爬取网易动态评论
- python爬网易评论
- 利用Python爬取基于AES对称加密算法的网易云音乐用户评论数据
- 爬取网易云音乐下面的热门评论
- 爬取网易云音乐评论
- 爬取网易云音乐评论2
- 100行python代码爬取5万条网易新闻评论
- Scrapy爬取网易云音乐和评论(二、Scrapy框架每个模块的作用)
- 用python写爬虫爬取腾讯视频的评论
- 爬取网易云音乐评论《安河桥》,进行分析
- 淘宝评论爬取 python pandas
- python学习,爬取淘宝评论数据
- python 多线程爬取考拉商品评论
- 对象3
- Android Intent信使使用
- 关于二叉树的所有操作
- 强化学习进阶 第八讲 确定性策略方法
- 对象4
- python爬取有关熊安新区的网易评论
- logback,logstash,elasticsearch配置,日志收集
- Java学习之使用Runtime.exec()启动、关闭Tomcat
- 如何解决Android中Hosts文件丢失或者说显示不了!(图解)
- 一个CTF工具分享站
- Treasure Exploration(POJ_2594) -有向图可重复点-最小路径覆盖
- 基于keras的二分类的网络训练代码
- java SHA1WithRSA 算法
- 动态规划:怪盗基德的滑翔翼