如何使用python抓取这个特定的jQuery站点?

2024-05-28 19:30:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想浏览这个网站:https://resultadoselecciones2016.onpe.gob.pe/PRP2V2016/Actas-por-Ubigeo.html

他们使用jQuery,所以数据不在“普通”html代码上。我在Chrome开发者控制台上看到了:

enter image description here

enter image description here

所以我在python 2.7上做了这个:

import urllib
import urllib2

url = 'https://resultadoselecciones2016.onpe.gob.pe/PRP2V2016/Actas-por-Ubigeo.html'

data = "pid=844399127479680.2&_clase=mesas&_accion=displayMesas&ubigeo=140107&nroMesa=034915&tipoElec=10&page=1&pornumero=1"

req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
print response.read()

但它不起作用,它只是打印普通的html,而不是上面看到的响应。在

如何获取这些数据?在


Tags: 数据httpsimporturldatahtmlurllib2pe
1条回答
网友
1楼 · 发布于 2024-05-28 19:30:28

我刚刚解决了这个问题。我使用了requests模块来代替urllib,只需复制/粘贴整个头,如下所示:

import requests
from bs4 import BeautifulSoup

url2 = "https://resultadoselecciones2016.onpe.gob.pe/PRP2V2016/ajax.php"
head = "[my entire header]"
data_get_departamentos = "pid=1037937475037058.5&_clase=ubigeo&_accion=getDepartamentos&dep_id=&tipoElec=&tipoC=acta&modElec=&ambito=E&pantalla="

r = requests.post(url2, data=data_get_departamentos, headers=head)
departamentos = r.text

然后我使用Beautifulsoup来解析html响应。这就是全部。在

相关问题 更多 >

    热门问题