lxml.html.读取文件时出错;无法加载外部实体 [英] lxml.html. Error reading file; Failed to load external entity

查看:783
本文介绍了lxml.html.读取文件时出错;无法加载外部实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lxml.html进行解析,从YouTube获取电影预告片的网址:

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]

我收到以下错误:

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.

推荐答案

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

SSL/TLS is not supported by libxml2. Use Python's urllib2 instead.

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

If you try any url with http://<blah>.<blah> you wont have trouble but https is not supported here. There are redirection issues also.

尝试

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

有关更多信息,请参见

For more information refer this

解决方案

Solution

有解决方法.尝试使用selenium,如果您不想使用UI,请以无头模式运行selenium.工作正常,我自己尝试过.

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.

这篇关于lxml.html.读取文件时出错;无法加载外部实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆