Python LXML网页抓取,格式错误的html

Python LXML网页抓取,格式错误的html,python,xml,web-scraping,Python,Xml,Web Scraping,我试图从这个网站上删除文章文本,但是它的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/2

我试图从这个网站上删除文章文本,但是它的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() 

lxml或格式错误的html没有问题,lxml的html解析器可以处理这一问题


您的代码工作正常,只是xpath表达式与任何内容都不匹配,所以
a
将是
None

使用其他解析器?BeautifulSoup可以使用不同的解析器,并为所有解析器提供相同的API(尽管不支持XPath)<代码>html5lib可以更好地完成页面的工作。