Python 如何防止LXML错误';无法加载外部实体';

Python 如何防止LXML错误';无法加载外部实体';,python,html,linux,parsing,lxml,Python,Html,Linux,Parsing,Lxml,我在使用lxml.html.parse()时遇到一些问题。: 这是我的代码(简称): 它基本上工作正常,但有时我会遇到很多这样的错误: 加载页面时出错:读取文件时出错 “b'”:b'无法加载外部 “实体” 加载页面时出错:读取文件时出错 ‘b’: b'加载HTTP资源失败' 加载页面时出错:读取文件时出错 “b'”:b'未能 加载外部实体 “' 我在这里研究了其他问题和答案,但他们所能建议的只是使用urllib——但在我尝试时,这并没有真正起到帮助 我想要的是禁用加载“外部实体”,不管它是什么意

我在使用
lxml.html.parse()时遇到一些问题。

这是我的代码(简称):

它基本上工作正常,但有时我会遇到很多这样的错误:

加载页面时出错:读取文件时出错 “b'”:b'无法加载外部 “实体”

加载页面时出错:读取文件时出错 ‘b’: b'加载HTTP资源失败'

加载页面时出错:读取文件时出错 “b'”:b'未能 加载外部实体 “'

我在这里研究了其他问题和答案,但他们所能建议的只是使用urllib——但在我尝试时,这并没有真正起到帮助


我想要的是禁用加载“外部实体”,不管它是什么意思。我想要的只是给定URL上的html。

当我用Wireshark嗅探时,我看到:

对于
http://twitter.com/wordpressdotcom

GET /wordpressdotcom HTTP/1.0
Host: twitter.com
Accept-Encoding: gzip

HTTP/1.0 301 Moved Permanently
content-length: 0
date: Sat, 01 Feb 2014 12:08:01 UTC
location: https://twitter.com/wordpressdotcom
server: tfe
set-cookie: guest_id=v1%3A139125648190241848; Domain=.twitter.com; Path=/; Expires=Mon, 01-Feb-2016 12:08:01 UTC
对于
http://www.amazon.com/gp/offer-listing/0375714634/ref=la_B001IGSNMM_1_9_cp_1_pap_olp/185-7720102-5178158?s=books&ie=UTF8&qid=1391249475&sr=1-9&condition=collectible

GET /gp/offer-listing/0375714634/ref=la_B001IGSNMM_1_9_cp_1_pap_olp/185-7720102-5178158?s=books&ie=UTF8&qid=1391249475&sr=1-9&condition=collectible HTTP/1.0
Host: www.amazon.com
Accept-Encoding: gzip

HTTP/1.1 503 Service Unavailable
Date: Sat, 01 Feb 2014 12:10:49 GMT
Server: Server
Last-Modified: Fri, 30 Nov 2012 01:26:22 GMT
ETag: "3dd-4cfac498acb80-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 599
Connection: close
Content-Type: text/html

...........SMo.0...Wp..]b....M...Pl.....`l.V+K..8M..(;.v.a..S..(.=.m....l.k.u......~...V....b.....j:.U...S.u."..k.|vy....J.P4..fY...x0..7....[Kp....S.Y.O...>B.GKk.c].....0/..wR9.ag.q...F...6hg....M....d........N.vk..Yi}8.r.......V..t
.... !...B.0..f.._9.G...\....OY0...-..{........xZ^.......n~.(8.:.k%1
Z2M+....[.5.Z.2.R..DL.KV.y2.Y...4N...z....Z.N....V........].DV.z^..}..j>W.;..WB.bS.......ba.3.g..G8......".}b...th1....a."`x........>[.@......8-........z.q.{.CJE.@>.d..?...UK...dQ'.J
....KW..v...iK.q.=-AI.?....za7.=/u/.......T.Sf}...\t.iJ. ..8.....U...dg...9..t#.g......Lz.. .?...i.........L]....
对于
http://plugins.trac.wordpress.org/changeset/559098

GET /changeset/559098 HTTP/1.0
Host: plugins.trac.wordpress.org
Accept-Encoding: gzip

HTTP/1.1 302 Found
Date: Sat, 01 Feb 2014 12:13:06 GMT
Server: Apache
Location: https://plugins.trac.wordpress.org/changeset/559098
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 242
Connection: close
Content-Type: text/html; charset=iso-8859-1

..........uO.N.0...+L..eh..L$X;1i@......fR.DI:...n.r.l?.....|.4.u./.......n..-..j..eS^..(...\fd..K2.t..1.,...l.4j.."#<.....3.N^..e.dc..m....F....5.....171......;.AD.Z.c.v.C..w..5v.8.r....\..L.. t..OEi=3..Sm.<.?.....e....*................|7...
GET/changeset/559098 HTTP/1.0
主持人:plugins.trac.wordpress.org
接受编码:gzip
找到HTTP/1.1 302
日期:2014年2月1日星期六12:13:06 GMT
服务器:Apache
地点:https://plugins.trac.wordpress.org/changeset/559098
改变:接受编码
内容编码:gzip
内容长度:242
连接:关闭
内容类型:text/html;字符集=iso-8859-1

uO.N.0…+L..eh..L$X;1i@......fR.DI:…n.r.l?…4.u./…n…-…j…eS^(…\fd..K2.t..1.,…l.4j.“#好吧,我想我会研究一下
请求
。但这不是一个真正的解决方案,我只是想禁用外部实体加载。您仍然可以使用
no\u network=True
实例化解析器(请参阅)但是我认为它在这里没有帮助,因为起始页无法加载(无法加载的是外部实体),没有链接的外部实体…哦
not_network=True
已经是默认值了知道python3是否存在
requests
吗?从项目页面上看,它看起来是这样的,
pip install requests
工作了,但我无法导入它。检查这些:,我最终使用了urllib,所以我想我可以关闭它
GET /changeset/559098 HTTP/1.0
Host: plugins.trac.wordpress.org
Accept-Encoding: gzip

HTTP/1.1 302 Found
Date: Sat, 01 Feb 2014 12:13:06 GMT
Server: Apache
Location: https://plugins.trac.wordpress.org/changeset/559098
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 242
Connection: close
Content-Type: text/html; charset=iso-8859-1

..........uO.N.0...+L..eh..L$X;1i@......fR.DI:...n.r.l?.....|.4.u./.......n..-..j..eS^..(...\fd..K2.t..1.,...l.4j.."#<.....3.N^..e.dc..m....F....5.....171......;.AD.Z.c.v.C..w..5v.8.r....\..L.. t..OEi=3..Sm.<.?.....e....*................|7...