2016-09-25 2 views
0

이 url의 두 번째 tbody에서 열 머리글을 가져와야합니다.여러 개의 HTML에서 열 머리글 얻기 'tbody'

runfile('C:/Python27/Lib/site-packages/xy/workspace/webscrape/mpob1.py', wdir='C:/Python27/Lib/site-packages/xy/workspace/webscrape') 
Traceback (most recent call last): 

    File "<ipython-input-8-ab4005f51fa3>", line 1, in <module> 
    runfile('C:/Python27/Lib/site-packages/xy/workspace/webscrape/mpob1.py', wdir='C:/Python27/Lib/site-packages/xy/workspace/webscrape') 

    File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile 
    execfile(filename, namespace) 

    File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 

    File "C:/Python27/Lib/site-packages/xy/workspace/webscrape/mpob1.py", line 26, in <module> 
    soup.findAll('tbody', limit=2)[1].findAll('tr').findAll('th')] 

IndexError: list index out of range 

여기 사람이 나를 도와주세요 수 있습니다

http://bepi.mpob.gov.my/index.php/statistics/price/daily.html

특히, 난 등 "9 월, 10 월"...

나는 다음과 같은 오류를 얻고을보고 싶다 아웃? 나는 영원히 감사 할 것이다!

import requests 

from bs4 import BeautifulSoup 

import pandas as pd 



url = "http://bepi.mpob.gov.my/index.php/statistics/price/daily.html" 



r = requests.get(url) 



soup = BeautifulSoup(r.text, 'lxml') 


column_headers = [th.getText() for th in 
       soup.findAll('tbody', limit=2)[1].findAll('tr').findAll('th')] 
+0

당신은 의미합니까, 당신은 단지 달 선택 요소의 내용이 필요하거나 실제로 필요 "View Price"를 클릭하고 "MPOB DAILY FFB REFERENCE PRICE SUMMARY BY REGION"테이블을 구문 분석 하시겠습니까? 감사합니다 – alecxe

+0

'가격보기'를 클릭해야합니다. 문제가되는 테이블은 '말레이시아 반도 : RBD P. Oil, RBD P.Olein & RBD P. Stearin'의 현지 가격 요약 –

답변

1

당신은 POST 요청이 http://bepi.mpob.gov.my/admin2/price_local_daily_view3.php 엔드 포인트로 전송됩니다 "보기 가격"버튼을 클릭하면 :

아래 내 코드를 게시했다.

import requests 
from bs4 import BeautifulSoup 


with requests.Session() as session: 
    session.get("http://bepi.mpob.gov.my/index.php/statistics/price/daily.html") 

    response = session.post("http://bepi.mpob.gov.my/admin2/price_local_daily_view3.php", data={ 
     "tahun": "2016", 
     "bulan": "9", 
     "Submit2222": "View Price" 
    }) 
    soup = BeautifulSoup(response.content, 'lxml') 

    table = soup.find("table", id="hor-zebra") 
    headers = [td.get_text() for td in table.find_all("tr")[2].find_all("td")] 
    print(headers) 

테이블의 헤더를 인쇄 : 결과 HTML을이 POST 요청을 시뮬레이션하고 분석

[u'Tarikh', u'September', u'October', u'November', u'December', u'September', u'October', u'November', u'December', u'September', u'October', u'November', u'December'] 
+0

은 완벽했습니다. 고맙습니다! –