import requests from bs4 import BeautifulSoup

请求链接

url = 'http://news.gqnu.edu.cn/' response = requests.get(url)

解析网页

soup = BeautifulSoup(response.content, 'html.parser') news_list = soup.find_all('li', class_='news-item')

爬取所有新闻的标题,正文和url地址

result = [] for news in news_list: title = news.h3.a.text.strip() # 获取新闻标题 url = news.h3.a['href'] # 获取新闻链接 response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') content = soup.find('div', class_='content').text.strip() # 获取新闻正文 result.append({'title': title, 'content': content, 'url': url})

保存到txt文件

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

广轻新闻爬取:提取标题、正文和链接并保存到txt文件

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

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