我想直接从博彩公司的网页上抓取他们的赌注。目前,我尝试从名为 unibet.com 的提供商处获取报价。问题:我需要发送一个发布请求,以便对我想要的报价进行适当的过滤。
因此我转到以下网页https://www.unibet.com/betting/grid/all-football/germany/bundesliga/1000094994.odds#投注部分的上部有几个复选框。我取消选中每个框而不是“匹配”。然后我单击更新按钮并使用 chrome 记录发布请求。以下屏幕截图演示了正在发送的内容:
之后,我得到一个过滤结果,其中仅包含匹配的引号。
现在,我只想得到这些引言。因此我编写了以下Python代码:
req = urllib2.Request( 'https://www.unibet.com/betting/grid/grid.do?eventGroupIds=1000094994' )
req.add_header("Content-type", "application/x-www-form-urlencoded")
post_data = [ ('format','iframe'),
('filtered','true'),
('gridSelectedTab','1'),
('_betOfferCategoryTab.filterOptions[1_604139].checked','true'),
('betOfferCategoryTab.filterOptions[1_604139].checked','on'),
('_betOfferCategoryTab.filterOptions[1_611318].checked','false'),
('_betOfferCategoryTab.filterOptions[1_611319].checked','false'),
('_betOfferCategoryTab.filterOptions[1_611321].checked','false'),
('_betOfferCategoryTab.filterOptions[1_604144].checked','false'),
('_betOfferCategoryTab.filterOptions[1_624677].checked','false'),
('_betOfferCategoryTab.filterOptions[1_604142].checked','false'),
('_betOfferCategoryTab.filterOptions[1_604145].checked','false'),
('_betOfferCategoryTab.filterOptions[1_611322].checked','false'),
('_betOfferCategoryTab.filterOptions[1_604148].checked','false'),
('gridSelectedTimeframe','')]
post_data = urllib.urlencode(post_data)
req.add_header('Content-Length', len(post_data ))
resp = urllib2.urlopen(req, post_data )
html = resp.read()
问题:我得到的是所有报价和投注类型的完整列表,而不是过滤结果,就好像所有复选框都已被选中一样。我不明白为什么我的 python 请求返回未经过滤的数据?
最佳答案
该网站将您的偏好存储在 session cookie 中。由于您没有捕获和发送适当的 cookie,因此网站更新后会显示其默认结果。
试试这个:
import cookielib
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(
urllib2.HTTPRedirectHandler(),
urllib2.HTTPHandler(debuglevel=0),
urllib2.HTTPSHandler(debuglevel=0),
urllib2.HTTPCookieProcessor(cookiejar),
)
现在,不用使用 urllib2.open()
,只需将 opener 作为函数调用来调用:opener()
并传递您的参数。
关于python - urllib2 发送 postdata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12447528/