前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python爬取百度页面的热搜榜

python爬取百度页面的热搜榜

作者头像
Python研究者
发布2020-10-29 16:50:17
3.2K0
发布2020-10-29 16:50:17
举报
文章被收录于专栏:Python研究者Python研究者
代码语言:javascript
复制
内容概览
python3简单爬取百度首页的热搜榜信息

爬取的页面如下:

地址:https://www.baidu.com/

代码如下:

代码语言:javascript
复制
# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup
from datetime import datetime

headers = {
    '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 "
}
response = requests.get("https://www.baidu.com/", headers=headers)
# 解析
bsObj = BeautifulSoup(response.text)
# 获取 response header时间
resDate = response.headers.get('Date')
print(resDate)
# 找到热搜榜
nameList = bsObj.findAll("li", {"class": {"hotsearch-item odd", "hotsearch-item even"}})
# 添加热搜榜的内容
tests = []
for name in nameList:
    tests.append(name.getText())
# 排序
tests.sort()
for news in tests:
    news = news[0:1] + " : " + news[1:]
    print(news)

打印出的结果如下:

欢迎关注公众号:Python爬虫数据分析挖掘

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-10-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python爬虫数据分析挖掘 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档