我正在尝试从此网站http://sana.sy/eng/21/2013/01/07/pr-460536.htm中删除文章文字,但其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 :(得分:0)
lxml或格式错误的html不是问题,lxml的html解析器可以处理它。
您的代码运行正常,只是您的xpath表达式与任何内容都不匹配,因此a
将为None
。