python爬取微博代码
时间: 2023-07-02 07:24:34 浏览: 117
python爬取新浪微博源代码
以下是使用Python爬取微博的示例代码:
```python
import requests
import json
# 登录微博账号获取cookie
login_url = "https://passport.weibo.cn/sso/login"
data = {
"username": "your_username",
"password": "your_password",
"savestate": "1",
"r": "",
"ec": "0",
"pagerefer": "",
"entry": "mweibo",
"wentry": "",
"loginfrom": "",
"client_id": "",
"code": "",
"qq": "",
"mainpageflag": "1",
"hff": "",
"hfp": ""
}
response = requests.post(login_url, data=data)
cookie_jar = response.cookies
# 获取微博内容
weibo_url = "https://m.weibo.cn/api/container/getIndex?type=uid&value=your_uid"
headers = {
"Referer": "https://m.weibo.cn/u/your_uid",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299"
}
response = requests.get(weibo_url, headers=headers, cookies=cookie_jar)
json_data = json.loads(response.text)
weibo_list = json_data['data']['cards']
# 解析微博内容
for weibo in weibo_list:
if weibo['card_type'] == 9:
mblog = weibo['mblog']
text = mblog['text']
print(text)
```
请注意,这只是一个示例代码,实际应用中需要根据自己的需求进行修改和优化,同时需要遵守相关网站的爬虫规则和法律法规。
阅读全文