import requests from bs4 import BeautifulSoup

url = 'http://news.gdqy.edu.cn/' response = requests.get(url) response.encoding = 'utf-8' soup = BeautifulSoup(response.text, 'html.parser')

news_list = []

for news in soup.select('.news_list ul li'): title = news.select_one('a').text url = news.select_one('a')['href'] response_news = requests.get(url) response_news.encoding = 'utf-8' soup_news = BeautifulSoup(response_news.text, 'html.parser') content = soup_news.select_one('.content').text.strip() news_list.append({'title': title, 'content': content, 'url': url})

with open('C:/Users/31973/OneDrive/桌面/实训(1)/json/news.txt', 'w', encoding='utf-8') as f: for news in news_list: f.write('标题:' + news['title'] + '\n') f.write('正文:' + news['content'] + '\n') f.write('链接:' + news['url'] + '\n\n')

Python 爬取广轻新闻页面并保存至文本文件

原文地址: https://www.cveoy.top/t/topic/oJ3N 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录