python爬取微博文本
2020-05-08 04:19
1516 查看
#! usr/bin/python # -*- coding=utf-8 -*- from urllib.parse import urlencode import requests from pyquery import PyQuery as pq import pandas as pd base_url='https://m.weibo.cn/api/container/getIndex?' headers={ 'Host':'m.weibo.cn', 'Referer':'https://m.weibo.cn/u/xxx', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36', 'X-Requested-With':'XMLHttpRequest' } def get_page(since_id): params={'uid':'xxx',#随需更改 't':'0', 'luicode':'xxx', 'type':'uid', 'value':'1862462415', 'containerid':'1076031862462415'} if since_id !=0: params['since_id']=since_id url=base_url+urlencode(params) print(url) try: response=requests.get(url,headers=headers) if response.status_code==200: return response.json() except requests.ConnectionError as e: print('error',e.args) def parse_page(json): if json: items=json.get('data').get('cards') for item in items: url=item.get('mblog').get('scheme') yemian=get_page(url) weibo={} weibo['id']=item.get('id') weibo['time']=item.get('created_at') weibo['text']=pq(item.get('text')).text() weibo['reposts']=item.get('reposts_count') weibo['comment']=item.get('comments_count') weibo['attitude']=item.get('attitudes_count') weibo['user']=item.get('user').get('screen_name') yield weibo if __name__ == '__main__': since_id=0 danny=[] for page in range(1,100): jsone=get_page(since_id) print(jsone) results=parse_page(jsone) for result in results: try: danny.append(result) except: print('='*10+"此内容无法显示"+"="*10) try: since_id=jsone['data']['cardlistInfo']['since_id'] except: print("到头了") danny_f=pd.DataFrame(danny) danny_f.to_excel(r'C:\Users\xxxx\Desktop\项目实战数据\xx.xls',encoding='utf-8-sig') print(danny_f)cyincy 原创文章 2获赞 0访问量 155 关注 私信
相关文章推荐
- python进行文本分类,基于word2vec,sklearn-svm对微博性别分类
- 利用python+jieba+gensim+sklearn实现微博文本性别分类
- Python数据采集与文本分析(学术)
- python获取文本网页
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
- [resource-]Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- 2017.8.7 用python实现简单文本编辑器
- SnowNLP:一个处理中文文本的 Python 类库
- Python爬虫scrapy框架发送POST请求以及自定义中间件(使用Cookie池,获取Cookie)——登录,爬取微博
- python读取数值文本数据比较--一般读取方式 Vs numpy
- python图片文本识别的简单实现
- 利用PYTHON快速统计数字|单词在文本中出现的次数
- Python 网络抓取和文本挖掘-1 HTML 和 HTMLParser
- python处理utf-16 le格式中文文本
- python 正则表达式过滤文本中的html标签 源代码解析
- Python + OpenCV实现基于傅里叶变换(FFT)的旋转文本校正(文字方向检测)
- Python模拟登陆万能法-微博|知乎
- Python调用微博API
- Python 小练习 文本替代