Python 爬虫爬取文章并保存到.txt文本中
·
1.代码
import requests
from bs4 import BeautifulSoup
import re
def fetch_page(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
else:
print(f"请求失败,状态码:{response.status_code}")
return None
def parse_content(html):
soup = BeautifulSoup(html, 'lxml')
data = soup.select('.Artical_Content') # 假设需要提取的元素有 class="detail-con"
return data
def save_to_file(data, file_path):
with open(file_path, 'a+', encoding="UTF-8") as file:
file.write("")
for item in data:
text = item.get_text().strip()
file.write(text + "\n")
def main():
url = "https://china.chinadaily.com.cn/a/202503/06/WS67c99c29a310510f19eea2b5.html"
html = fetch_page(url)
if html:
data = parse_content(html)
file_path = 'E:\\新闻内容.txt'
save_to_file(data, file_path)
print("数据已成功保存到文件!")
if __name__ == "__main__":
main()
2.运行结果


更多推荐



所有评论(0)