学习建议
<div class="c-single-text-ellipsis"> 绿我涓滴 会它千顷澄碧 <!--38--></div> <div class="c-single-text-ellipsis"> 英媒称有人目击凯特现身 <!--48--></div>
<div class="c-single-text-ellipsis">(.\*?)</div>
<div class="hot-desc\_1m\_jR small\_Uvkd3 ellipsis\_DupbZ"> 如今兰考发生了翻天覆地的变化,张庄村的老百姓把xx走过的一条路改名为“幸福路”,沿着“幸福路”奔向越来越好的日子... <a xxxx t;</a></div> <div class="hot-desc\_1m\_jR small\_Uvkd3 ellipsis\_DupbZ"> 17日,广东深圳一女子在山顶为打卡拍照徒手攀爬时不慎手滑险些落山。当地办事处表示雨天路滑不建议攀爬,正常山道是有人看管的... <a yyyy ></a></div>
<div class="hot-desc\_1m\_jR small\_Uvkd3 ellipsis\_DupbZ">(.\*)<a
div class="hot-index_1Bl1a">(.*?)</div>
URL:url = https://top.baidu.com/board?tab=realtime热搜标题: title = re.compile(r'<div class="c-single-text-ellipsis">(.*?)</div>')热搜简介:introduction = re.compile(r'<div class="hot-desc\_1m\_jR small\_Uvkd3 ellipsis\_DupbZ">(.*)<a')热搜指数:index = re.compile(r'<div class="hot-index\_1Bl1a">(.*?)</div>')
根据以上分析,我们整理下思路:
详细代码如下:
from bs4 import BeautifulSoup
import re
import urllib.request, urllib.error
class TestHotsearch():
def __init__(self):
# 热搜URL
self.url = 'https://top.baidu.com/board?tab=realtime'
# 热搜标题
self.title = re.compile(r'<div class="c-single-text-ellipsis">(.*?)</div>')
# 热搜简介
self.introduction = re.compile(r'<div class="hot-desc_1m_jR small_Uvkd3 ellipsis_DupbZ">(.*)<a')
# 热搜指数
self.index = re.compile(r'<div class="hot-index_1Bl1a">(.*?)</div>')
# 所有热搜条目
self.all_content = "category-wrap_iQLoo horizontal_1eKyQ"
def test_html_content(self):
"""
获取热搜页面的html内容
:return:
"""
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
}
request = urllib.request.Request(self.url, headers = header)
html_content = ""
try:
response = urllib.request.urlopen(request)
html_content = response.read().decode("utf-8")
except urllib.error.URLError as e:
if hasattr(e, "code"):
print(e.code)
if hasattr(e, "reason"):
print(e.reason)
return html_content.encode('gbk', 'ignore').decode('gbk')
def test_get_content(self):
"""
获取需要的重点信息
:return:
"""
# 获取html内容
html = self.test_html_content()
# 定义一个空列表保存要获取的信息
data_info = []
content = BeautifulSoup(html, "html.parser")
for name in content.find_all('div', class_=self.all_content):
data = []
name_str = str(name)
title = re.findall(self.title, name_str)
data.append(title)
introduction = re.findall(self.introduction, name_str)
data.append(introduction)
index = re.findall(self.index, name_str)
data.append(index)
data_info.append(data)
return data_info
if __name__ == "__main__":
hot_search = TestHotsearch()
get_content = hot_search.test_get_content()
print(f"获取到信息如下:{get_content}")
获取到信息如下:[[' 心系这门“关键课程” ', [], ' 4932922 '], [' 三只羊就梅菜扣肉事件致歉 ', [], ' 4991528 '], [' 女子山顶徒手攀爬石头手滑摔下 ', [], ' 4816630 '], [' 春分将至农事忙 ', [], ' 4790902 '],.........
Python主要是简单的爬虫实战,步骤清晰,容易理解和入门。建议最好用自己本地环境测试,仅供学习参考,请勿做其他用途。重点是学习Python正则表达式的应用,python的BeautifulSoup、request模块的使用等。
腾讯云【云直播 CSS】
https://cloud.tencent.com/product/css
云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。