如何使用python撰寫爬蟲程式-自動搜尋關鍵字取得yahoo新聞的標題及網址

 

1. 安裝Requests和BeautifulSoup及lxml

pip install beautifulsoup4
pip install requests
pip install lxml

2. 撰寫程式碼:
=====以下是程式碼內容=====

 $ cat yahoonews.py 
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
keywords_ids = open("newskeywordslist.txt").read().splitlines()
for keywords_id in keywords_ids:
	print('\n\n搜尋關鍵字:'+keywords_id+'=======================================')
	url = 'https://tw.news.yahoo.com/search?p='+keywords_id
	#url = 'https://tw.news.yahoo.com/topic/2019-nCoV'
	res = requests.get(url,headers=headers)
	# 確認是否成功
	if res.status_code == requests.codes.ok:
		# 以 BeautifulSoup 解析 HTML 程式碼
		soup = BeautifulSoup(res.text, 'html.parser')
		stories = soup.select('a[href]')
		for s in stories:
			if keywords_id in s.text:
				# 新聞標題
				print("標題:" + s.text)
				# 新聞網址
				a_string = s.get('href')
				substring1="yahoo"
				##substring2="html"
				if substring1 in a_string :
					print(a_string)
				else:
					print('https://tw.news.yahoo.com'+a_string)

在Linux或是macOS底下,為了方便使用,第一行直接宣告環境變數,讓python程式可以和shell script有一樣的執行方式

3. 想要搜尋的關鍵字列表:
=====以下是檔名及內容格式=====

$ cat newskeywordslist.txt
新冠肺炎
武漢肺炎
川普

4. 執行結果如下:

$ ./yahoonews.py 


搜尋關鍵字:新冠肺炎=======================================
標題: 新冠肺炎
https://tw.news.yahoo.com/topic/2019-nCoV
標題:新冠肺炎疫情爆發時間軸 旅遊警示全球疫情總整理
https://tw.news.yahoo.com/%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E%E8%B6%85%E9%81%8Esars%E5%85%A8%E7%90%83%E7%97%85%E4%BE%8B%E6%95%B8-%E7%96%AB%E6%83%85%E7%88%86%E7%99%BC%E6%99%82%E9%96%93%E8%BB%B8-095814929.html
標題:新冠肺炎疫情看不見盡頭?台大醫師提3理由預估:台灣疫情將趨緩
https://tw.news.yahoo.com/%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E7%96%AB%E6%83%85%E7%9C%8B%E4%B8%8D%E8%A6%8B%E7%9B%A1%E9%A0%AD-%E5%8F%B0%E5%A4%A7%E9%86%AB%E5%B8%AB%E6%8F%903%E7%90%86%E7%94%B1%E9%A0%90%E4%BC%B0-%E5%8F%B0%E7%81%A3%E7%96%AB%E6%83%85%E5%B0%87%E8%B6%A8%E7%B7%A9-013001722.html
標題:新冠肺炎衝擊 美股斷融後3類App下載量飆升
https://tw.news.yahoo.com/%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E8%A1%9D%E6%93%8A-%E7%BE%8E%E8%82%A1%E6%96%B7%E8%9E%8D%E5%BE%8C3%E9%A1%9Eapp%E4%B8%8B%E8%BC%89%E9%87%8F%E9%A3%86%E5%8D%87-030325917.html
標題:驚!美MIT最新研究:防染新冠肺炎 至少要離8米遠
https://tw.news.yahoo.com/%E9%A9%9A%E7%BE%8Emit%E6%9C%80%E6%96%B0%E7%A0%94%E7%A9%B6%E9%98%B2%E6%9F%93%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E-%E8%87%B3%E5%B0%91%E8%A6%81%E9%9B%A2-8-%E7%B1%B3%E9%81%A0-014248816.html
標題:【新冠肺炎即時動態】台灣最新疫情資訊一次看
https://tw.news.yahoo.com/%E4%B8%8D%E6%96%B7%E6%9B%B4%E6%96%B0%E6%8E%8C%E6%8F%A1%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E7%AC%AC%E4%B8%80%E6%89%8B%E7%96%AB%E6%83%85%E8%B3%87%E8%A8%8A-105035951.html
標題:新冠肺炎引爆「蟲蟲危機」!全球最大保險套製造商:保險套短缺1億個
https://tw.news.yahoo.com/%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E5%BC%95%E7%88%86-%E8%9F%B2%E8%9F%B2%E5%8D%B1%E6%A9%9F-%E5%85%A8%E7%90%83%E6%9C%80%E5%A4%A7%E4%BF%9D%E9%9A%AA%E5%A5%97%E8%A3%BD%E9%80%A0%E5%95%86-%E4%BF%9D%E9%9A%AA%E5%A5%97%E7%9F%AD%E7%BC%BA1%E5%84%84%E5%80%8B-023001743.html
標題:快訊/紐西蘭首起新冠肺炎死亡 南島75歲婦人
https://tw.news.yahoo.com/%E5%BF%AB%E8%A8%8A-%E7%B4%90%E8%A5%BF%E8%98%AD%E9%A6%96%E8%B5%B7%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E6%AD%BB%E4%BA%A1-%E5%8D%97%E5%B3%B675%E6%AD%B2%E5%A9%A6%E4%BA%BA-032610828.html
標題:新冠肺炎台灣確診身份關係圖 史上最清楚一秒看懂【不斷更新】
https://tw.news.yahoo.com/%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E%E5%8F%B0%E7%81%A3%E7%A2%BA%E8%A8%BA%E8%BA%AB%E4%BB%BD%E9%97%9C%E4%BF%82%E5%9C%96-%E5%8F%B2%E4%B8%8A%E6%9C%80%E6%B8%85%E6%A5%9A%E4%B8%80%E7%A7%92%E7%9C%8B%E6%87%82%E4%B8%8D%E6%96%B7%E6%9B%B4%E6%96%B0-031339185.html
標題:新冠肺炎致死率不高,得了不會怎麼樣?──病毒背後,你看不到的骨牌效應
https://tw.news.yahoo.com/%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E8%87%B4%E6%AD%BB%E7%8E%87%E4%B8%8D%E9%AB%98%E5%BE%97%E4%BA%86%E4%B8%8D%E6%9C%83%E6%80%8E%E9%BA%BC%E6%A8%A3%E7%97%85%E6%AF%92%E8%83%8C%E5%BE%8C%E4%BD%A0%E7%9C%8B%E4%B8%8D%E5%88%B0%E7%9A%84%E9%AA%A8%E7%89%8C%E6%95%88%E6%87%89-230057232.html
標題:新冠肺炎恐透過氣溶膠傳染?張上淳這樣說
https://tw.news.yahoo.com/%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E6%81%90%E9%80%8F%E9%81%8E%E6%B0%A3%E6%BA%B6%E8%86%A0%E5%82%B3%E6%9F%93-%E5%BC%B5%E4%B8%8A%E6%B7%B3%E9%80%99%E6%A8%A3%E8%AA%AA-005001349.html
標題:倦怠、堅毅、溫柔的勇者面容 他們是義大利抗擊新冠肺炎的第一線戰士
https://tw.news.yahoo.com/%E5%80%A6%E6%80%A0-%E5%A0%85%E6%AF%85-%E6%BA%AB%E6%9F%94%E7%9A%84%E5%8B%87%E8%80%85%E9%9D%A2%E5%AE%B9-%E4%BB%96%E5%80%91%E6%98%AF%E7%BE%A9%E5%A4%A7%E5%88%A9%E6%8A%97%E6%93%8A%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E7%9A%84%E7%AC%AC-%E7%B7%9A%E6%88%B0%E5%A3%AB-003001642.html
標題:快訊/美嬰兒新冠肺炎確診後死亡 死因釐清中
https://tw.news.yahoo.com/%E5%BF%AB%E8%A8%8A-%E7%BE%8E%E5%AC%B0%E5%85%92%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E7%A2%BA%E8%A8%BA%E5%BE%8C%E6%AD%BB%E4%BA%A1-%E6%AD%BB%E5%9B%A0%E9%87%90%E6%B8%85%E4%B8%AD-002023981.html
標題:超前部署 雲縣新冠肺炎防疫車隊成立
https://tw.news.yahoo.com/%E8%B6%85%E5%89%8D%E9%83%A8%E7%BD%B2-%E9%9B%B2%E7%B8%A3%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E9%98%B2%E7%96%AB%E8%BB%8A%E9%9A%8A%E6%88%90%E7%AB%8B-162200405.html
標題:日職/糟了!跟藤浪一起用餐2女性 同時確診新冠肺炎
https://tw.news.yahoo.com/%E6%97%A5%E8%81%B7-%E7%B3%9F%E4%BA%86-%E8%B7%9F%E8%97%A4%E6%B5%AA-%E8%B5%B7%E7%94%A8%E9%A4%902%E5%A5%B3%E6%80%A7-%E5%90%8C%E6%99%82%E7%A2%BA%E8%A8%BA%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E-160011268.html


搜尋關鍵字:武漢肺炎=======================================
標題:義甲因武漢肺炎停賽 祖文特斯球員教練同意減薪
https://tw.news.yahoo.com/%E7%BE%A9%E7%94%B2%E5%9B%A0%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E%E5%81%9C%E8%B3%BD-%E7%A5%96%E6%96%87%E7%89%B9%E6%96%AF%E7%90%83%E5%93%A1%E6%95%99%E7%B7%B4%E5%90%8C%E6%84%8F%E6%B8%9B%E8%96%AA-025003277.html
標題:【武漢肺炎】北京「人道救援」外宣破功 中國廠商遭爆出口劣質試劑、拉抬10倍價格
https://tw.news.yahoo.com/%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E-%E5%8C%97%E4%BA%AC-%E4%BA%BA%E9%81%93%E6%95%91%E6%8F%B4-%E5%A4%96%E5%AE%A3%E7%A0%B4%E5%8A%9F-%E4%B8%AD%E5%9C%8B%E5%BB%A0%E5%95%86%E9%81%AD%E7%88%86%E5%87%BA%E5%8F%A3%E5%8A%A3%E8%B3%AA%E8%A9%A6%E5%8A%91-020200629.html
標題:武漢肺炎》喵星人也會感染?防檢局:尚無證據能傳播
https://tw.news.yahoo.com/%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E-%E5%96%B5%E6%98%9F%E4%BA%BA%E4%B9%9F%E6%9C%83%E6%84%9F%E6%9F%93-%E9%98%B2%E6%AA%A2%E5%B1%80-%E5%B0%9A%E7%84%A1%E8%AD%89%E6%93%9A%E8%83%BD%E5%82%B3%E6%92%AD-020329317.html
標題:義大利武漢肺炎新增889死 累計不治破萬
https://tw.news.yahoo.com/%E7%BE%A9%E5%A4%A7%E5%88%A9%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E%E6%96%B0%E5%A2%9E889%E6%AD%BB-%E7%B4%AF%E8%A8%88%E4%B8%8D%E6%B2%BB%E7%A0%B4%E8%90%AC-004020787.html
標題:極罕見病例 美國一嬰兒死於武漢肺炎
https://tw.news.yahoo.com/%E6%A5%B5%E7%BD%95%E8%A6%8B%E7%97%85%E4%BE%8B-%E7%BE%8E%E5%9C%8B-%E5%AC%B0%E5%85%92%E6%AD%BB%E6%96%BC%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E-000940692.html
標題:美武漢肺炎病亡逾2000人 川普擬封鎖紐約地區
https://tw.news.yahoo.com/%E7%BE%8E%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E%E7%97%85%E4%BA%A1%E9%80%BE2000%E4%BA%BA-%E5%B7%9D%E6%99%AE%E6%93%AC%E5%B0%81%E9%8E%96%E7%B4%90%E7%B4%84%E5%9C%B0%E5%8D%80-032005181.html
標題:【武漢肺炎/不斷更新】190國確診近65萬例、30178死 美15州重大災難狀態 義破萬死全球最多 華航今晚接回湖北台人
https://tw.news.yahoo.com/%E4%B8%8D%E6%96%B7%E6%9B%B4%E6%96%B0%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E%E5%85%A8%E7%90%83%E7%96%AB%E6%83%85-%E7%A2%BA%E8%A8%BA%E7%97%85%E4%BE%8B%E7%A0%B42000%E5%A4%A7%E9%99%B856%E4%BA%BA%E6%AD%BB%E4%BA%A1-081134694.html


搜尋關鍵字:川普=======================================
標題:美武漢肺炎病亡逾2000人 川普擬封鎖紐約地區
https://tw.news.yahoo.com/%E7%BE%8E%E6%AD%A6%E6%BC%A2%E8%82%BA%E7%82%8E%E7%97%85%E4%BA%A1%E9%80%BE2000%E4%BA%BA-%E5%B7%9D%E6%99%AE%E6%93%AC%E5%B0%81%E9%8E%96%E7%B4%90%E7%B4%84%E5%9C%B0%E5%8D%80-032005181.html
標題:紐約逾700死 川普:紐約3州不必隔離 改發旅遊警示
https://tw.news.yahoo.com/%E7%B4%90%E7%B4%84%E9%80%BE700%E6%AD%BB-%E5%B7%9D%E6%99%AE-%E7%B4%90%E7%B4%843%E5%B7%9E%E4%B8%8D%E5%BF%85%E9%9A%94%E9%9B%A2-%E6%94%B9%E7%99%BC%E6%97%85%E9%81%8A%E8%AD%A6%E7%A4%BA-024055088.html
標題:政策急轉彎! 川普改口:沒必要隔離紐約.新澤西.康州
https://tw.news.yahoo.com/%E6%94%BF%E7%AD%96%E6%80%A5%E8%BD%89%E5%BD%8E-%E5%B7%9D%E6%99%AE%E6%94%B9%E5%8F%A3-%E6%B2%92%E5%BF%85%E8%A6%81%E9%9A%94%E9%9B%A2%E7%B4%90%E7%B4%84-%E6%96%B0%E6%BE%A4%E8%A5%BF-%E5%BA%B7%E5%B7%9E-015004808.html
標題:隔離措施州長跳腳 川普只發旅遊警示
https://tw.news.yahoo.com/%E9%9A%94%E9%9B%A2%E6%8E%AA%E6%96%BD%E5%B7%9E%E9%95%B7%E8%B7%B3%E8%85%B3-%E5%B7%9D%E6%99%AE%E5%8F%AA%E7%99%BC%E6%97%85%E9%81%8A%E8%AD%A6%E7%A4%BA-014203143.html
標題:不隔離紐約新澤西和康州了 川普:改發旅遊警示
https://tw.news.yahoo.com/%E4%B8%8D%E9%9A%94%E9%9B%A2%E7%B4%90%E7%B4%84%E6%96%B0%E6%BE%A4%E8%A5%BF%E5%92%8C%E5%BA%B7%E5%B7%9E%E4%BA%86-%E5%B7%9D%E6%99%AE-%E6%94%B9%E7%99%BC%E6%97%85%E9%81%8A%E8%AD%A6%E7%A4%BA-013502013.html
標題:快新聞/紐約州、新澤西州、康州不隔離了! 川普推特發文:改發布旅遊警示
https://tw.news.yahoo.com/%E5%BF%AB%E6%96%B0%E8%81%9E-%E7%B4%90%E7%B4%84%E5%B7%9E-%E6%96%B0%E6%BE%A4%E8%A5%BF%E5%B7%9E-%E5%BA%B7%E5%B7%9E%E4%B8%8D%E9%9A%94%E9%9B%A2%E4%BA%86-%E5%B7%9D%E6%99%AE%E6%8E%A8%E7%89%B9%E7%99%BC%E6%96%87-014638372.html
標題:避免引發恐慌 川普急轉彎決定不封紐約地區
https://tw.news.yahoo.com/%E9%81%BF%E5%85%8D%E5%BC%95%E7%99%BC%E6%81%90%E6%85%8C-%E5%B7%9D%E6%99%AE%E6%80%A5%E8%BD%89%E5%BD%8E%E6%B1%BA%E5%AE%9A%E4%B8%8D%E5%B0%81%E7%B4%90%E7%B4%84%E5%9C%B0%E5%8D%80-013814868.html
標題:川普揚言「封鎖美3州」 急改口:強制隔離沒必要
https://tw.news.yahoo.com/%E5%B7%9D%E6%99%AE%E6%8F%9A%E8%A8%80-%E5%B0%81%E9%8E%96%E7%BE%8E3%E5%B7%9E-%E6%80%A5%E6%94%B9%E5%8F%A3-%E5%BC%B7%E5%88%B6%E9%9A%94%E9%9B%A2%E6%B2%92%E5%BF%85%E8%A6%81-005342471.html
標題:防止疫情擴散 川普考慮隔離紐約、新澤西及康乃狄克3州
https://tw.news.yahoo.com/%E9%98%B2%E6%AD%A2%E7%96%AB%E6%83%85%E6%93%B4%E6%95%A3-%E5%B7%9D%E6%99%AE%E8%80%83%E6%85%AE%E9%9A%94%E9%9B%A2%E7%B4%90%E7%B4%84-%E6%96%B0%E6%BE%A4%E8%A5%BF%E5%8F%8A%E5%BA%B7%E4%B9%83%E7%8B%84%E5%85%8B3%E5%B7%9E-002131163.html
標題:快新聞/美國病死數破2000!川普考慮對紐約州等州採短期隔離
https://tw.news.yahoo.com/%E5%BF%AB%E6%96%B0%E8%81%9E-%E7%BE%8E%E5%9C%8B%E7%97%85%E6%AD%BB%E6%95%B8%E7%A0%B42000-%E5%B7%9D%E6%99%AE%E8%80%83%E6%85%AE%E5%B0%8D%E7%B4%90%E7%B4%84%E5%B7%9E%E7%AD%89%E5%B7%9E%E6%8E%A1%E7%9F%AD%E6%9C%9F%E9%9A%94%E9%9B%A2-232028357.html
標題:美國確診破12萬!川普:考慮強制隔離紐約、紐澤西、康乃狄克州
https://tw.news.yahoo.com/%E7%BE%8E%E5%9C%8B%E7%A2%BA%E8%A8%BA%E7%A0%B412%E8%90%AC-%E5%B7%9D%E6%99%AE-%E8%80%83%E6%85%AE%E5%BC%B7%E5%88%B6%E9%9A%94%E9%9B%A2%E7%B4%90%E7%B4%84-%E7%B4%90%E6%BE%A4%E8%A5%BF-%E5%BA%B7%E4%B9%83%E7%8B%84%E5%85%8B%E5%B7%9E-233551171.html
標題:川普轉彎了,蔡英文呢
https://tw.news.yahoo.com/%E5%B7%9D%E6%99%AE%E8%BD%89%E5%BD%8E%E4%BA%86-%E8%94%A1%E8%8B%B1%E6%96%87%E5%91%A2-201010947.html
標題:川普昔日大讚習近平 被疫情逼急又轉而批陸
https://tw.news.yahoo.com/%E5%B7%9D%E6%99%AE%E6%98%94%E6%97%A5%E5%A4%A7%E8%AE%9A%E7%BF%92%E8%BF%91%E5%B9%B3-%E8%A2%AB%E7%96%AB%E6%83%85%E9%80%BC%E6%80%A5%E5%8F%88%E8%BD%89%E8%80%8C%E6%89%B9%E9%99%B8-133259487.html
標題:【確診破10萬】川普下令增產醫療設備
https://tw.news.yahoo.com/%E7%A2%BA%E8%A8%BA%E7%A0%B410%E8%90%AC-%E5%B7%9D%E6%99%AE%E4%B8%8B%E4%BB%A4%E5%A2%9E%E7%94%A2%E9%86%AB%E7%99%82%E8%A8%AD%E5%82%99-160000416.html
標題:川普計畫「隔離」大紐約3州 葛謨:沒談過 不知道什麼意思
https://tw.news.yahoo.com/%E5%B7%9D%E6%99%AE-%E8%A8%88%E7%95%AB%E5%B0%8D%E5%A4%A7%E7%B4%90%E7%B4%84%E5%9C%B0%E5%8D%80%E5%B0%81%E5%9F%8E2%E5%91%A8-054400063.html
標題:川普改口:大紐約3州沒必要隔離 要CDC發布「強力旅遊警示」
https://tw.news.yahoo.com/%E5%B7%9D%E6%99%AE%E6%94%B9%E5%8F%A3-%E5%A4%A7%E7%B4%90%E7%B4%84%E5%9C%B0%E5%8D%80%E5%BC%B7%E5%88%B6%E9%9A%94%E9%9B%A2-%E9%9D%9E%E5%BF%85%E8%A6%81-134200379.html
標題:美確診破10萬 川普啟動「國防生產法」增產呼吸器
https://tw.news.yahoo.com/%E7%BE%8E%E7%A2%BA%E8%A8%BA%E7%A0%B410%E8%90%AC-%E5%B7%9D%E6%99%AE%E5%95%9F%E5%8B%95-%E5%9C%8B%E9%98%B2%E7%94%9F%E7%94%A2%E6%B3%95-%E5%A2%9E%E7%94%A2%E5%91%BC%E5%90%B8%E5%99%A8-040642040.html
標題:川普:100天造10萬台呼吸器 有餘可助歐洲抗疫
https://tw.news.yahoo.com/%E5%B7%9D%E6%99%AE-100%E5%A4%A9%E9%80%A010%E8%90%AC%E5%8F%B0%E5%91%BC%E5%90%B8%E5%99%A8-%E6%9C%89%E9%A4%98%E5%8F%AF%E5%8A%A9%E6%AD%90%E6%B4%B2%E6%8A%97%E7%96%AB-132454948.html
標題:川普致電慰問 親曝強森開口要這配備
https://tw.news.yahoo.com/%E5%B7%9D%E6%99%AE%E8%87%B4%E9%9B%BB%E6%85%B0%E5%95%8F-%E8%A6%AA%E6%9B%9D%E5%BC%B7%E6%A3%AE%E9%96%8B%E5%8F%A3%E8%A6%81%E9%80%99%E9%85%8D%E5%82%99-092127201.html
標題:美確診破10萬! 川普簽署66兆緊急紓困法案
https://tw.news.yahoo.com/%E7%BE%8E%E7%A2%BA%E8%A8%BA%E7%A0%B410%E8%90%AC-%E5%B7%9D%E6%99%AE%E7%B0%BD%E7%BD%B266%E5%85%86%E7%B7%8A%E6%80%A5%E7%B4%93%E5%9B%B0%E6%B3%95%E6%A1%88-064335054.html

延伸閱讀:

如何使用python撰寫爬蟲程式-自動取得當日即時股價

發表留言