找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2018|回复: 2

[代码与实例] 爬取微博热搜代码

1

主题

1

帖子

1

积分

贫民

积分
1
古城 发表于 2021-4-26 14:39:16 | 显示全部楼层 |阅读模式
import requests
from lxml import etree
import time
url='https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr='
header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'}
resp = requests.get (url,headers=header)
resp1 = resp.content.decode(encoding='utf-8',errors='ignore')
resp2=etree.HTML(resp1)
title = resp2.xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr/td/a/text()')
print (time.strftime("%F,%R")+'微博热搜\n')
for i in range(51):
    print ('  '.join([title[i]]),'\n')
    time.sleep(1)
回复

使用道具 举报

0

主题

1

帖子

1

积分

贫民

积分
1
no1024 发表于 2022-1-28 16:22:09 | 显示全部楼层
  print ('  '.join([title[i]]),'\n')
IndexError: list index out of range
回复 支持 反对

使用道具 举报

0

主题

8

帖子

8

积分

贫民

积分
8
hnchshlily 发表于 2022-6-16 10:34:33 | 显示全部楼层
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/2.py", line 2, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表