標籤

2022年11月21日 星期一

中時新聞網-python解析新聞標題連結

 今天將中時新聞網改用python來解析新聞標題連結

https://www.chinatimes.com/realtimenews/?chdtv

用chrome來看一下網頁原始碼

<div class="col"><h3 class="title"><a href="/realtimenews/20221121001555-260410">台版晶片法獨厚特定廠商?王美花:子法草案盡快出爐</a></h3><div class="meta-info"><time datetime="2022-11-21 10:49"><span class="hour">10:49</span><span class="date">2022/11/21</span></time><div class="category"><a href="/realtimenews/260410">財經</a></div></div><p class="intro">行政院通過修正《產創條例》新增10條之2,提高高科技業研發費用抵減營所稅25%,被外界稱為「台版晶片法」,不過卻遭外界質疑,獨厚台積電。經濟部長王美花表示,條文並無限制只有半導體產業,例如5G、電動車</p></div>


主要要抓的標題及連結會在 <h3 class="title">這里面

所以python 這麼寫,就可取得所有了
for news in soup.find_all("h3",{"class":"title"}):
print(news)
a = news.find('a')
title = a.string
url = a['href']
print('-' * 80)
print ("Found the URL:", url,title)

沒有留言:

張貼留言