Skip to main content
 首页 » 编程设计

python之在 python 中用 lxml 解析 http[s] weboages

2024年10月01日36mfrbuaa

直到最近我才能够从网站 here 解析表格数据.我注意到传输协议(protocol)从 http 更改为 https,我认为这就是原因,旧代码不再有效。我得到这个错误:IOError: Error reading file 'https:/.... 我的代码基本上是

import lxml.html 
page = lxml.html.parse(url)  
table = [page.xpath("//tr/td/text()") 

如何继续从 https 网页获取数据,最好坚持使用 lxml?

请您参考如下方法:

你可以这样使用它:

import lxml.html as lh 
import requests as r 
 
 
doc = r.get(yourHttpsUrl).content 
html = lh.fromstring(doc) 
 
# now use your xpath on this `html`