问题
I am trying to get a movie trailer url from YouTube using parsing with lxml.html:
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]
I get the following error:
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"
The exact same way of doing to parse information from other websites and it worked then.
回答1:
SSL/TLS is not supported by libxml2. Use Python's urllib2 instead.
If you try any url with http://<blah>.<blah>
you wont have trouble but https is not supported here. There are redirection issues also.
Try
from urllib2 import urlopen
import lxml.html
tree = lxml.html.parse(urlopen('https://google.com'))
For more information refer this
Solution
Well there are workaround. Try selenium and if you dont want a UI then run selenium in headless mode. Works fine i tried it myself.
来源:https://stackoverflow.com/questions/29421666/lxml-html-error-reading-file-failed-to-load-external-entity