1 Star 0 Fork 184

GSAM / Python爬虫

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
(Ajax请求)爬取微博 正文 点赞数 转发数 评论数 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
from pyquery import PyQuery as pq
import requests
import urllib.parse as p
#自己写入微博的URL 这个是Ajax接口Url
base_url='https://m.weibo.cn/api/container/getIndex?'
headers={
'改成自己的User-Agent',
'X-Requested-With':'XMLHttpRequest'
}
def get(page):
params={
'type': 'uid',
'value': '2830678474',
'containerid': '1076032830678474',
'page': page
}
url=base_url+p.urlencode(params)
try:
response=requests.get(url,headers=headers)
if response.status_code == 200:
return response.json()
except requests.ConnectionError as e:
print('error:',e)
def parse_set(json):
if json:
items=json.get('data').get('cards')
print('此时的items是啥:',items)
for i in items:
item=i.get('mblog')
weibo={}
#还可添加别的条目
weibo['id']=item.get('id')
weibo['正文']=pq(item.get('text')).text()
weibo['点赞数目']=item.get('attitudes_count')
weibo['评论数']=item.get('comments_count')
weibo['转发数:']=item.get('reposts_count')
#生成器
yield weibo
if __name__=='__main__':
for page in range(2,11):
json=get(page)
test=parse_set(json)
for t in test:
print(t)
Python
1
https://gitee.com/dushuren/python_reptilian.git
git@gitee.com:dushuren/python_reptilian.git
dushuren
python_reptilian
Python爬虫
master

搜索帮助