Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing Tables? #111

Open
NYamaguchi415 opened this issue Dec 9, 2015 · 5 comments
Open

Accessing Tables? #111

NYamaguchi415 opened this issue Dec 9, 2015 · 5 comments

Comments

@NYamaguchi415
Copy link

Is there any way to access information from a table on a page?

I can go through the html and use beautifulsoup, but the tables on some pages don't have unique identifiers that I can use to select specific tables and it's become a bit of a pain.

@vesper8
Copy link

vesper8 commented Dec 12, 2016

did you ever figure this out? needing to do the same

@scholi
Copy link

scholi commented May 17, 2018

same here. Does some of you have a solution?

@stella-lu
Copy link

^ same

@gibbbone
Copy link

I have solved by making a function from scratch to export tables.

I'm using MediaWikiAPI which is a more updated fork of this project. It has the same structure so I think you should get the same results by substituting MediaWikiAPI with wikipedia

Sample of how it works:

# load page 
mediawikiapi = MediaWikiAPI()
test_page = mediawikiapi.page(PageWithTables)

# scrape the HTML with BeautifulSoup to find tables
soup = BeautifulSoup(test_page.html(), 'html.parser')
tables = soup.findAll("table", { "class" : "wikitable" })

# select target table and apply custom function to export it to pandas
target_table = tables[0]
df_test = wikitable_to_dataframe(target_table)

Here's the full procedure and the function wikitable_to_dataframe :
https://gist.github.com/giovannibonaccorsi/6ba30ec92894130c67258ffc6e09c9a4#file-export_wikipedia_tables_to_pandas-py

@stevans
Copy link

stevans commented Sep 14, 2020

@gibbbone Thanks!

The gist you posted to the wikitable_to_dataframe code is dead, but I found that you can use pandas to create a list of the tables as datafames:

import pandas as pd
pd.read_html(test_page.url, attrs={"class": "wikitable"})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants