本文最後更新於 1163 天前,其中的信息可能已經有所發展或是發生改變。
爬取重慶交通大學官網首頁的時政要聞,並提取信息,將時政要聞的時間、標題全部導出顯示
import requests
from urllib import request
from bs4 import BeautifulSoup
def main():
url = "http://news.cqjtu.edu.cn/rss.htm"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
page = request.Request(url, headers=headers)
page_info = request.urlopen(page).read()
soup = BeautifulSoup(page_info, 'html.parser')
news_titles = soup.select("#ywDiv_0 a")
news_titles.extend(soup.select("#JxDiv_0 a"))
news_titles.extend(soup.select("#JDiv_0 a"))
news_titles.extend(soup.select("#JKDiv_0 a"))
print(type(news_titles))
for news in news_titles:
#print(news)
title = news.get_text()
link = news.get("href")
data = {"title": title, "link": "http://news.cqjtu.edu.cn"+link}
print(data)
main()
Post Views: 370