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'可以更好地完成頁面。 – 2013-04-21 18:12:56