wikipediaからテーブルを掻き取ろうとしています。私は、パンダのデータフレームとしてテーブルをダウンロードして保存するウェブで利用可能なチュートリアルを使用してテーブルスクレーパを書きました。Pythonを使用したWikipediaテーブルの掻き取り
これは、コード
from bs4 import BeautifulSoup
import pandas as pd
import urllib2
headers = { 'User-Agent' : 'Mozilla/5.0' }
req = urllib2.Request('https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population', None, headers)
html = urllib2.urlopen(req).read()
soup = BeautifulSoup(html, 'lxml') # Parse the HTML as a string
print soup
# Create an object of the first object
table = soup.find("table", {"class":"wikitable sortable jquery-tablesorter"})
print table
rank=[]
country=[]
pop=[]
date=[]
per=[]
source=[]
for row in table.find_all('tr')[1:]:
col=row.find_all('td')
col1=col[0].string.strip()
rank.append(col1)
col2=col[1].string.strip()
country.append(col2)
col3=col[2].string.strip()
pop.append(col2)
col4=col[3].string.strip()
date.append(col4)
col5=col[4].string.strip()
per.append(col5)
col6=col[5].string.strip()
source.append(col6)
columns={'Rank':rank,'Country':country,'Population':pop,'Date':date,'Percentage':per,'Source':source}
# Create a dataframe from the columns variable
df = pd.DataFrame(columns)
df
である。しかし、それはテーブルをダウンロードされていません。問題は、このセクションで
table = soup.find("table", {"class":"wikitable sortable jquery-tablesorter"})
print table
ある出力は私の知る限り見ることができるようにNone
ウィキペディアは完全に優れたAPIを持っていますが、どうしてページを削っていますか? –
私はちょうどPythonを使用してWebページのスクレイピングについて学習しており、私はテストページとしてwikipediaを使用しています – Eka