请用Python写出一个爬取微博用户关注列表的程序代码
时间: 2023-03-14 21:12:47 浏览: 85
import requests# 定义请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}# 获取要爬取的微博用户页面地址
url = 'https://weibo.com/******'
# 获取该用户的关注列表
follow_url = url + '/follow'# 发起请求,获取响应
response = requests.get(follow_url, headers=headers)
# 获取响应的文本数据
html = response.text# 使用正则表达式提取关注列表
import re
pattern = r'<a class="W_texta W_fb" target="_blank"\s+href="(.*?)">(.*?)</a>'
res = re.findall(pattern, html)# 打印关注列表
for item in res:
print(item[1], item[0])
相关问题
请用Python写出一个爬取微博用户“邓超”关注列表的程序代码
import requests
import json# 获取access_token
url = 'https://api.weibo.com/oauth2/access_token'
payload = {'client_id': 'your_client_id',
'client_secret': 'your_client_secret',
'grant_type': 'client_credentials'}
r = requests.post(url, data=payload)
result = json.loads(r.text)
access_token = result.get('access_token')# 获取邓超的uid
url = 'https://api.weibo.com/2/users/show.json'
payload = {'access_token': access_token,
'screen_name': '邓超'}
r = requests.get(url, params=payload)
result = json.loads(r.text)
uid = result.get('id')# 获取邓超的关注列表
url = 'https://api.weibo.com/2/friendships/friends/bilateral.json'
payload = {'access_token': access_token,
'uid': uid}
r = requests.get(url, params=payload)
result = json.loads(r.text)
follow_list = result.get('users')# 打印出关注列表
for follow in follow_list:
print(follow.get('screen_name'))
请为我书写一个爬取微博用户关注列表的Python代码
# 以下是一个爬取微博用户关注列表的Python代码,请根据需要进行修改:import requests
from lxml import etreeurl = 'https://weibo.com/{user_id}/follow'header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
'Referer': 'https://weibo.com/u/{user_id}/home?wvr=5'
}def get_following_list(user_id):
following_list = [] response = requests.get(url.format(user_id=user_id), headers=header)
html = etree.HTML(response.text)
following_items = html.xpath('//div[@class="follow_inner"]/ul/li/dl/dt/a')
for item in following_items:
user_name = item.xpath('./text()')[0]
user_id = item.xpath('./@usercard')[0].split('=')[1]
following_list.append({
'user_name': user_name,
'user_id': user_id
})
return following_listif __name__ == "__main__":
following_list = get_following_list('{user_id}')
for user in following_list:
print(user)