提取百度热搜的标题和对应的链接url,输出或保存到文件中。
时间: 2023-10-09 12:03:01 浏览: 163
提取百度热搜的标题和对应的链接URL可以使用Python的爬虫技术来实现。
首先,我们需要安装和导入相关的库。使用pip命令安装requests和beautifulsoup4库:
```
pip install requests
pip install beautifulsoup4
```
然后,我们可以编写Python代码来提取百度热搜的标题和对应的链接URL:
```python
import requests
from bs4 import BeautifulSoup
def get_hot_searches():
url = "https://top.baidu.com/board?tab=realtime"
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
hot_searches = soup.find_all('a', class_='list-title')
for hot_search in hot_searches:
title = hot_search.text.strip()
link = hot_search['href']
print(title, link)
# 调用函数
get_hot_searches()
```
以上代码会打印出百度热搜的标题和对应的链接URL。
如果要将数据保存到文件中,我们可以使用Python的文件操作来实现:
```python
import requests
from bs4 import BeautifulSoup
def get_hot_searches():
url = "https://top.baidu.com/board?tab=realtime"
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
hot_searches = soup.find_all('a', class_='list-title')
with open('hot_searches.txt', 'w', encoding='utf-8') as file:
for hot_search in hot_searches:
title = hot_search.text.strip()
link = hot_search['href']
file.write(title + ' ' + link + '\n')
# 调用函数
get_hot_searches()
```
以上代码会将百度热搜的标题和对应的链接URL保存到名为"hot_searches.txt"的文件中。
阅读全文
相关推荐











