python 爬取深圳十大热点的例程
时间: 2024-11-09 22:27:32 浏览: 26
Python爬取深圳十大热点内容通常会涉及网络爬虫技术,使用如requests库获取网页数据,BeautifulSoup或PyQuery解析HTML结构,提取所需信息。下面是一个简单的示例,假设我们想爬取一个新闻网站的热门文章列表:
```python
导入必要的库
import requests from bs4 import BeautifulSoup
指定目标网址,这里以一个虚构的新闻站点为例
url = "https://example.com/shenzhen/hot"
def get_hot_topics(url): # 发送GET请求 response = requests.get(url)
# 判断请求是否成功
if response.status_code == 200:
# 解析HTML页面
soup = BeautifulSoup(response.text, 'html.parser')
# 找到包含热点标题的区域(这将取决于实际网站的结构)
hot_articles = soup.select('.hot-article-list > article') # 假设CSS选择器
topics = []
for article in hot_articles:
title = article.find('h2').text # 获取标题
link = article.find('a')['href'] # 获取链接
topics.append((title, link))
return topics
else:
print(f"无法访问 {url},状态码:{response.status_code}")
return []
调用函数获取并打印结果
hot_topics = get_hot_topics(url) for index, topic in enumerate(hot_topics, start=1): print(f"{index}. {topic[0]} - {topic[1]}")
相关推荐
















