这是我关于堆栈溢出的第一个问题,请耐心等待。在
我正在尝试从网站自动下载(例如,抓取)一些意大利法律的文本:http://www.normattiva.it/
我使用下面的代码(以及类似的排列):
import requests, sys
debug = {'verbose': sys.stderr}
user_agent = {'User-agent': 'Mozilla/5.0', 'Connection':'keep-alive'}
url = 'http://www.normattiva.it/atto/caricaArticolo?art.progressivo=0&art.idArticolo=1&art.versione=1&art.codiceRedazionale=047U0001&art.dataPubblicazioneGazzetta=1947-12-27&atto.tipoProvvedimento=COSTITUZIONE&art.idGruppo=1&art.idSottoArticolo1=10&art.idSottoArticolo=1&art.flagTipoArticolo=0#art'
r = requests.session()
s = r.get(url, headers=user_agent)
#print(s.text)
print(s.url)
print(s.headers)
print(s.request.headers)
如您所见,我正在尝试加载“caricarticolo”查询。在
但是,输出是一个页面,说明我的搜索无效(“session is not valid or expired”)
页面似乎意识到我没有使用浏览器,并加载了一个“breakout”javascript函数。在
^{pr2}$我尝试使用“浏览器”模拟器python脚本,例如selenium和robobrowser,但结果是一样的。在
是否有人愿意花10分钟时间查看页面输出并提供帮助?在
在“开发工具打开”页面上单击任何链接后,在“网络”下的“文档”选项卡下:
您可以看到三个链接,第一个链接是我们单击的内容,第二个链接返回允许您跳转到特定文章的html,最后一个链接包含文章文本。在
在firstlink返回的源代码中,可以看到两个iframe标记:
第一个是本文,后者带有/caricarticolodefault和id主机就是我们想要的。在
您需要使用来自初始请求的cookies,这样您就可以使用Session对象,并使用bs4解析页面:
^{pr2}$第一个文本文件的片段:
^{3}$太棒了,太棒了,太棒了。它起作用了。只需要稍微编辑一下就可以清除进口,但效果很好。非常感谢。我只是在发现python的潜力,您让我的旅程更轻松了。我不可能独自解决它。在
相关问题 更多 >
编程相关推荐