Python(BS4)Wiki页面抓取

时间:2020-09-01 08:44:21

标签: python html web-scraping beautifulsoup

我只想从Wiki页面上抓取电影标题,请帮助我

我的代码:

url = 'https://en.wikipedia.org/wiki/List_of_American_films_of_2020'
page = requests.get(url)
soup = BeautifulSoup(page.content,'html.parser')
movies = soup.find('table',{'class':'wikitable sortable'})
print(movies)

我只想从结构中过滤掉电影标题,就像图像中的电影标题应该只是“丢失的传输”

this is the structure of one movie in the HTML:

2 个答案:

答案 0 :(得分:1)

您可以进一步处理废弃的表格。

table_body = movies.find('tbody') 
titles = [] 
rows = table_body.find_all('tr') 
for row in rows[1:]: # leaving the first row, seems it is a header
    title_cell = row.select("td i a") 
    titles.append(title_cell[0].contents[0]) 
print(titles)     

答案 1 :(得分:0)

这实际上取决于您所追求的。如果您只想抓取that页面,则有一种更简便的方法:

import pandas as pd
pd.read_html("https://en.wikipedia.org/wiki/List_of_American_films_of_2020")

,它将为您获取<table></table HTML标记内该页面中包含的所有信息。 如果您想从Wikipedia获得更多信息,则也不必使用网络抓取,因为这里有一个非常有用的API。 如果您的目标是学习网络爬虫(请始终牢记robots.txt),则可以查看this教程,它对我有很大帮助。

相关问题