请为我书写一个爬取微博用户关注列表的Python代码
时间: 2023-03-14 14:12:47 浏览: 112
# 以下是一个爬取微博用户关注列表的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)
阅读全文