1

我想直接从他们的网页上抓取博彩公司的投注。目前,我尝试从名为 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 请求返回未过滤的数据?

4

1 回答 1

1

该网站将您的偏好存储在会话 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()只使用 call opener 作为函数调用:opener()并传递你的参数。

于 2012-11-01T16:42:24.067 回答