Попробуйте использовать bs4, чтобы получить информацию из Википедии - PullRequest
0 голосов
/ 08 марта 2020

Я начал изучать python в этом году как новогодние решения, PI столкнулся с некоторыми проблемами при самообучении веб-скребков. Это могут быть глупые вопросы, но я надеюсь, что кто-то может указать на проблемы моих кодов. Заранее спасибо!

Хочу веб-скребок из Википедии Nobel Economi c Приз https://en.wikipedia.org/wiki/List_of_Nobel_Memorial_Prize_laureates_in_Economics

# I first get the whole table
wiki_table = soup.find('table',{'class':'wikitable'})    

print(wiki_table)
# And grab the td information
name_list = wiki_table('td') 
print(name_list) 
type(name_list) #bs4.element.ResultSet
type(name_list[0:]) # list

# My goal is to separate laureate's name from other descriptions i.e. countries, years...What I plan to do is first get some lists containing people's names and then clean others unwanted strings. 
# I tried to loop both the bs4 type and list type 

laurates=[]
for a in name_list:
    laurates.append(name_list.find_all(class='a'))
print(laurates)

# I looped for a here because the html is like `<a href="/wiki/Ragnar_Frisch" title="Ragnar Frisch">Ragnar Frisch</a>`. I thought the name is with the a code (or I interpreted wrongly?)

1 Ответ

1 голос
/ 08 марта 2020

Самый простой способ (в данном случае) - просто загрузить таблицу в pandas фрейм данных и затем извлечь из нее все необходимые элементы, используя обычные pandas методы. Поэтому

import pandas as pd
url = "https://en.wikipedia.org/wiki/List_of_Nobel_Memorial_Prize_laureates_in_Economics"

pd.read_html(url)

выведет таблицу на этой странице.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...