首页 > 其他 > 详细

爬取搜狗热搜榜前十

时间:2020-03-20 00:05:54      阅读:64      评论:0      收藏:0      [点我收藏+]

1.导入相应的库

2.打开页面:http://top.sogou.com/(搜狗热搜榜)

3.用F12查找页面代码,找到我们要的数据

4.class="hot-b",class="num"

5.利用代码得出我们想要的数据

#导入库
import requests
import pandas as pd
from bs4 import BeautifulSoup
from pandas import DataFrame
url="http://top.sogou.com/"#搜狗热搜榜
headers = {User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/69.0.3497.100 Safari/537.36}#伪装爬虫
r=requests.get(url)#请求网站
r.encoding=r.apparent_encoding#统一编码
html = r.text
soup = BeautifulSoup(html,lxml)#使用工具
title=[]
heat=[]
for m in soup.find_all(class_="hot-b"):
    title.append(m.get_text().strip())  
for n in soup.find_all(class_="num"):
    heat.append(n.get_text().strip())
print(data)
a=pd.DataFrame(data,index=["标题","热度"])
print(a.T)

技术分享图片

 

 

技术分享图片技术分享图片

 

爬取搜狗热搜榜前十

原文:https://www.cnblogs.com/jiang0606/p/12528178.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!