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