2013-04-21 2 views
0

본 웹 사이트 http://sana.sy/eng/21/2013/01/07/pr-460536.htm에서 기사 텍스트를 긁어 내려고했지만 해당 HTML의 형식이 잘못되었습니다. 누구나 저에게 올바른 방법을 보여줄 수 있습니까?LXML 웹 페이지 스크래핑, 형식이 잘못된 html

this is the code 
import urllib2 
from lxml import etree 
import StringIO 

speachesurls = ["http://sana.sy/eng/21/2013/01/07/pr-460536.htm", "http://sana.sy/eng/21/2012/06/04/pr-423234.htm", "http://sana.sy/eng/21/2012/01/12/pr-393338.htm"] 


# scrape the speaches 

for url in speachesurls: 
    result = urllib2.urlopen(url) 
    html = result.read() 
    parser = etree.HTMLParser() 
    tree = etree.parse(StringIO.StringIO(html), parser) 
    xpath = "//html/body/table[3]/tbody/tr[3]/td[4]/table[2]/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td/div/p" 
    a = tree.find(xpath) 
    print a.text_content() 
+0

다른 파서를 사용 하시겠습니까? BeautifulSoup는 다른 파서를 사용할 수 있으며 모든 파서에 대해 동일한 API를 제공 할 수 있습니다 (XPath는 지원하지 않지만). 'html5lib'는 페이지의 더 나은 작업을 잘 할 수 있습니다. –

답변

0

이 LXML 또는 잘못된 HTML, LXML의 HTML 파서 그와 거래에 문제가 아니에요.

코드가 제대로 작동합니다. xpath 표현식이 아무 것도 아니므로 aNone이됩니다.