lxml.html。读取文件时出错;无法加载外部实体

时间:2015-04-02 20:13:45

标签: parsing lxml lxml.html

我正试图通过使用lxml.html解析来获取YouTube上的电影预告片:

from lxml import html
import lxml.html
from lxml.etree import XPath

def get_youtube_trailer(selected_movie):
# Create the url for the YouTube query in order to find the movie trailer
title = selected_movie
t = {'search_query' : title + ' movie trailer'}
query_youtube = urllib.urlencode(t)
search_url_youtube = 'https://www.youtube.com/results?' + query_youtube

# Define the XPath for the YouTube movie trailer link
movie_trailer_xpath = XPath('//ol[@class="item-section"]/li[1]/div/div/div[2]/h3/a/@href')

# Parse the YouTube html code
html = lxml.html.parse(search_url_youtube)

# Add the movie trailer to our results
results['movie_trailer'] = 'https://www.youtube.com' + movie_trailer_xpath(html)[0]

我收到以下错误:

File "C:/Users/Aleks/Google Drive/Udacity - Full Stack Web Dev Nanodegree/Lessons/Lesson 3a (Make Classes) - Movie Website/models.py", line 163, in <module>
print get_youtube_trailer("titanic")

File "C:/Users/Aleks/Google Drive/Udacity - Full Stack Web Dev Nanodegree/Lessons/Lesson 3a (Make Classes) - Movie Website/models.py", line 157, in get_youtube_trailer
html = lxml.html.parse(search_url_youtube)
File "C:\Python27\lib\site-packages\lxml\html\__init__.py", line 788, in parse
return etree.parse(filename_or_url, parser, base_url=base_url, **kw)
File "lxml.etree.pyx", line 3301, in lxml.etree.parse (src\lxml\lxml.etree.c:72453)
File "parser.pxi", line 1791, in lxml.etree._parseDocument (src\lxml\lxml.etree.c:105915)
File "parser.pxi", line 1817, in lxml.etree._parseDocumentFromURL (src\lxml\lxml.etree.c:106214)
File "parser.pxi", line 1721, in lxml.etree._parseDocFromFile (src\lxml\lxml.etree.c:105213)
File "parser.pxi", line 1122, in lxml.etree._BaseParser._parseDocFromFile (src\lxml\lxml.etree.c:100163)
File "parser.pxi", line 580, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:94286)
File "parser.pxi", line 690, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:95722)
File "parser.pxi", line 618, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:94754)
IOError: Error reading file 'https://www.youtube.com/results?search_query=titanic+movie+trailer': failed to load external entity "https://www.youtube.com/results?search_query=titanic+movie+trailer"

完全相同的方法来解析来自其他网站的信息,然后就可以了。

1 个答案:

答案 0 :(得分:5)

  

libxml2不支持SSL / TLS。请改用Python的urllib2。

如果您尝试使用http://<blah>.<blah>的任何网址,则不会遇到问题,但此处不支持https。还有重定向问题。

尝试

from urllib2 import urlopen
import lxml.html
tree = lxml.html.parse(urlopen('https://google.com'))

有关更多信息,请参阅this

<强> 解决方案

有解决方法。尝试使用selenium,如果你不想要UI,那么在无头模式下运行selenium。工作正常,我自己尝试过。