import requests from bs4 import BeautifulSoup url='https://top.baidu.com/board?tab=realtime' headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.37'} res = requests.get(url=url, headers=headers) res.encoding ='utf-8' soup = BeautifulSoup(res.text, 'html.parser') hot_list = soup.find_all(class_='c-single-text-ellipsis') print(hot_list) for hot in hot_list: print(hot.get_text().strip())在这段代码的基础上,利用open()函数将爬取内容写入txt文件
时间: 2024-03-19 13:40:49 浏览: 90
import reimport requestsfrom bs4 import BeautifulSoupimport t
好的,你可以在代码最后加上以下内容来将爬取内容写入txt文件:
```
with open('hot_list.txt', 'w', encoding='utf-8') as f:
for hot in hot_list:
f.write(hot.get_text().strip() + '\n')
```
这段代码会将爬取到的热搜内容写入一个名为"hot_list.txt"的文本文件中,每个热搜占一行。你可以根据需要修改文件名和路径。
阅读全文