2017-01-10 1 views
1

나는 테이블을 긁어서 파이썬으로 data.tables로 변환하려고하는데, 나는 미국에서 선거 데이터가 거의 없다. 이것은 내가 긁어 내고 싶은 데이터의 html입니다.파이썬 테이블 긁힌 자국

<tr class="type-republican"> 
<th class="results-name" scope="row"><a href="xxxxx"><span class="name-combo"><span class="token token-party"><abbr title="Republican">R</abbr></span> <span class="token token-winner"><b aria-hidden="true" class="icon icon-check"></b> <span class="icon-text">Winner</span></span> D. Trump</span></a></th> 
<td class="results-percentage"><span class="percentage-combo"><span class="number">62.9%</span><span class="graph"><span class="bar"><span class="index" style="width:62.9%;"></span></span></span></span></td> 
<td class="results-popular">1,306,925</td> 
<td class="delegates-cell">9</td> 
</tr> 
<tr class="type-democrat"> 
<th class="results-name" scope="row"><a href="xxxxxx"><span class="name-combo"><span class="token token-party"><abbr title="Democratic">D</abbr></span> H. Clinton</span></a></th> 
<td class="results-percentage"><span class="percentage-combo"><span class="number">34.6%</span><span class="graph"><span class="bar"><span class="index" style="width:34.6%;"></span></span></span></span></td> 
<td class="results-popular">718,084</td> 
<td class="delegates-cell"></td> 
</tr> 
<tr class="type-independent"> 
<th class="results-name" scope="row"><span class="name-combo"><span class="token token-party"><abbr title="Independent">I</abbr></span> G. Johnson</span></th> 
<td class="results-percentage"><span class="percentage-combo"><span class="number">2.1%</span><span class="graph"><span class="bar"><span class="index" style="width:2.1%;"></span></span></span></span></td> 
<td class="results-popular">43,869</td> 
<td class="delegates-cell"></td> 
</tr> 
<tr class="type-independent"> 
<th class="results-name" scope="row"><span class="name-combo"><span class="token token-party"><abbr title="Independent">I</abbr></span> J. Stein</span></th> 
<td class="results-percentage"><span class="percentage-combo"><span class="number">0.4%</span><span class="graph"><span class="bar"><span class="index" style="width:0.4%;"></span></span></span></span></td> 
<td class="results-popular">9,287</td> 
<td class="delegates-cell"></td> 
</tr> 
</tbody> 
</table>, <table class="results-table"> 
<tbody> 
<tr class="type-republican"> 
<th class="results-name" scope="row"><a href="xxxxx"><span class="name-combo"><span class="token token-party"><abbr title="Republican">R</abbr></span> D. Trump</span></a></th> 
<td class="results-percentage"><span class="percentage-combo"><span class="number">73.4%</span><span class="graph"><span class="bar"><span class="index" style="width:73.4%;"></span></span></span></span></td> 
<td class="results-popular">18,110</td> 
</tr> 
<tr class="type-democrat"> 
<th class="results-name" scope="row"><a href="xxxxxx"><span class="name-combo"><span class="token token-party"><abbr title="Democratic">D</abbr></span> H. Clinton</span></a></th> 
<td class="results-percentage"><span class="percentage-combo"><span class="number">24.0%</span><span class="graph"><span class="bar"><span class="index" style="width:24.0%;"></span></span></span></span></td> 
<td class="results-popular">5,908</td> 
</tr> 
<tr class="type-independent"> 
<th class="results-name" scope="row"><span class="name-combo"><span class="token token-party"><abbr title="Independent">I</abbr></span> G. Johnson</span></th> 
<td class="results-percentage"><span class="percentage-combo"><span class="number">2.2%</span><span class="graph"><span class="bar"><span class="index" style="width:2.2%;"></span></span></span></span></td> 
<td class="results-popular">538</td> 
</tr> 
<tr class="type-independent"> 
<th class="results-name" scope="row"><span class="name-combo"><span class="token token-party"><abbr title="Independent">I</abbr></span> J. Stein</span></th> 
<td class="results-percentage"><span class="percentage-combo"><span class="number">0.4%</span><span class="graph"><span class="bar"><span class="index" style="width:0.4%;"></span></span></span></span></td> 
<td class="results-popular">105</td> 
</tr> 
</tbody> 

등등 ... 그래서 내 코드는 다음과 같습니다.

Percentage = [] 
Count = [] 
page = requests.get('xxxx') 
soup = BeautifulSoup(page.text, "lxml") 
table = soup.find('div', class_='content-alpha') 
for row in table.find_all('tr'): 
    col = row.find_all('td') 
    Percentage = col[0].find(text=True) 
    Count = col[1].find(text=True 
    print (Count) 

하지만 여기서 알 수있는 것은 몇 가지 테이블의 정보이지만 모든 정보는 아닙니다. 모든 테이블에서 정보를 얻으려면 어떻게해야합니까? 그리고 왜 나는 몇 개의 테이블에서 정보를 얻습니까?

질문에 대한 이해를 바랍니다.

HTML이 너무 커서 웹 사이트 http://www.politico.com/2016-election/results/map/president/alabama/에 대한 링크를 추가합니다. 나는 알라바마

+0

'content-alpha'클래스가 여기에 데이터에 없습니다. 스크래핑하려는 데이터와 예상되는 결과를 업데이트 할 수 있습니까? – Stergios

+0

당신이 긁어 모으려는 URL을 제공하면 우리가 당신을 도울 수 있습니다. – wpercy

+0

나는 웹 사이트에 대한 링크를 추가했습니다. – Extria

답변

1

그래서 잠시 후 나는이 웹 사이트의 모든 데이터를 긁어 냈다. 그래서 주요 문제는 웹 사이트가 자바 스크립트에 내장되어있어 Beautifulsoup로 긁어 모을 수 없다는 것입니다. 그래서 나는 html로 페이지를 변환하고 그것을 긁어 모으기 위해 셀레늄 + beautifulsoup4를 사용했다.

from selenium import webdriver 
import time 
import os 
from bs4 import BeautifulSoup 
chrome_path = r"C:\Users\Desktop\chromedriver_win32\chromedriver.exe" 
driver = webdriver.Chrome(chrome_path) 
driver.get('http://www.politico.com/2016-election/primary/results/map/president/arizona/') 
time.sleep(80) 
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 
time.sleep(5) 
html = driver.page_source 
soup = BeautifulSoup(html,'html.parser') 
for posts in soup.findAll('table',{'class':'results-table'}): 
for tr in posts.findAll('tr'): 
    popular = [td for td in tr.stripped_strings] 
    print(popular) 

동적 인 웹 페이지이므로 셀렌으로 몇 가지를 시뮬레이트해야했습니다. 페이지를 아래로 스크롤하는 것과 같습니다. 페이지를로드 할 수 있도록 time.sleep (60)을 사용했습니다. 그것은 정말로 천천히로드됩니다. 그래서 나는 60s로 시간을 정했습니다. 누군가가 도움이되기를 바랍니다.

0
import requests, bs4 

r = requests.get('http://www.politico.com/2016-election/results/map/president/alabama/') 
soup = bs4.BeautifulSoup(r.text, 'lxml') 
contents = soup.find(class_='contrast-white') 
for table in contents.find_all(class_='results-group'): 
    title = table.find(class_='title').text 
    for tr in table.find_all('tr'): 
     _, name, percentage, popular = [td for td in tr.stripped_strings] 
     print(title, name, percentage, popular) 

아웃 모든 카운티의 2016 미국 선거 데이터를 긁어 싶지 :

Autauga County D. Trump 73.4% 18,110 
Autauga County H. Clinton 24.0% 5,908 
Autauga County G. Johnson 2.2% 538 
Autauga County J. Stein 0.4% 105 
Baldwin County D. Trump 77.4% 72,780 
Baldwin County H. Clinton 19.6% 18,409 
Baldwin County G. Johnson 2.6% 2,448 
Baldwin County J. Stein 0.5% 453 
Barbour County D. Trump 52.3% 5,431 
Barbour County H. Clinton 46.7% 4,848 
Barbour County G. Johnson 0.9% 93 
Barbour County J. Stein 0.2% 18 
Bibb County D. Trump 77.0% 6,733 
Bibb County H. Clinton 21.4% 1,874 
Bibb County G. Johnson 1.4% 124 
Bibb County J. Stein 0.2% 17 
Blount County D. Trump 89.9% 22,808 
Blount County H. Clinton 8.5% 2,150 
Blount County G. Johnson 1.3% 337 
Blount County J. Stein 0.4% 89 
Bullock County H. Clinton 75.1% 3,530 
Bullock County D. Trump 24.2% 1,139 
Bullock County G. Johnson 0.5% 22 
Bullock County J. Stein 0.2% 10 
Butler County D. Trump 56.3% 4,891 
Butler County H. Clinton 42.8% 3,716 
Butler County G. Johnson 0.7% 65 
Butler County J. Stein 0.1% 13 
Calhoun County D. Trump 69.2% 32,803 
Calhoun County H. Clinton 27.9% 13,197 
Calhoun County G. Johnson 2.4% 1,114 
Calhoun County J. Stein 0.6% 262 
Chambers County D. Trump 56.6% 7,803 
Chambers County H. Clinton 41.8% 5,763 
Chambers County G. Johnson 1.2% 168 
Chambers County J. Stein 0.3% 44 
Cherokee County D. Trump 83.9% 8,809 
Cherokee County H. Clinton 14.5% 1,524 
Cherokee County G. Johnson 1.4% 145 
Cherokee County J. Stein 0.2% 25 

enter image description here 나머지는 비어있는, 거기에 아무것도.

+0

답변 해 주셔서 감사합니다. 저는 Python을 처음 사용하기 때문에 체로키 카운티까지 페이지의 일부만 긁어 모으는 이유는 무엇입니까? – Extria

+0

@Extria 내 대답을 업데이트합니다. –

+0

그래서 나머지 카운티를 긁을 방법이 없습니까? – Extria

관련 문제