我正在嘗試使用 python 抓取維基百科頁面的一個小節中的鏈接。例如在這個: https ://en.wikipedia.org/wiki/Lists_of_video_games我只想獲取“按型別”部分下的所有鏈接。
我曾嘗試使用 beautifulsoup,但我得到的資訊太多,我需要一種方法來將我的回復僅限于該小節。
如果我還可以獲得小節標題會更好,例如“動作”中的所有鏈接,“體育”中的所有鏈接......等等。
任何幫助或指導將不勝感激
謝謝,
uj5u.com熱心網友回復:
希望,以下示例將是您想要的輸出
import requests
from bs4 import BeautifulSoup
url='https://en.wikipedia.org/wiki/Lists_of_video_games'
res= requests.get(url)
soup = BeautifulSoup(res.content,'lxml')
links = soup.select('.toclevel-1.tocsection-41> ul > li > a')
for link in links:
href= 'https://en.wikipedia.org/wiki/Lists_of_video_games' link.get('href')
print(href)
輸出:
https://en.wikipedia.org/wiki/Lists_of_video_games#Action
https://en.wikipedia.org/wiki/Lists_of_video_games#Casual_and_puzzle
https://en.wikipedia.org/wiki/Lists_of_video_games#Role-playing
https://en.wikipedia.org/wiki/Lists_of_video_games#Simulation
https://en.wikipedia.org/wiki/Lists_of_video_games#Sports
https://en.wikipedia.org/wiki/Lists_of_video_games#Strategy
https://en.wikipedia.org/wiki/Lists_of_video_games#Other
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/494907.html
