【问题标题】:Sending POST request to AJAX with Python3.4 and urllib使用 Python3.4 和 urllib 向 AJAX 发送 POST 请求
【发布时间】:2014-09-26 04:33:23
【问题描述】:

我正在尝试从 http://scores.covers.com/football-scores-matchups.aspx 刮线运动

我需要遍历使用提供的日历的每个季节和周:

当我检查网络以查看发送的内容时,我在更改季节时看到两个 POST 请求:

(我将以上 3 张图片合并为 1 张,因为我没有足够的代表来发布超过 1 张图片)

http://s29.postimg.org/773muclra/covers_scrape4.jpg

我整天都在搜索、阅读和破解它,但没有取得任何进展。如果我将有效负载编辑为意外内容,我可以通过使用 Firebug 编辑并重新发送发布请求来重新创建我遇到的错误。所以我觉得问题出在我编码和发送数据的方式上。我已经尝试了 json.dump、utf-8、Content-Type application/x-www...,在我能想到的每一种组合中。

我目前的代码如下:

import urllib.request
import json
import urllib.parse
class Scraper():

    def __init__(self):
        pass

    def scrape(self):
        url = 'http://scores.covers.com/ajax/SportsDirect.Controls.LiveScoresControls.ScoresCalendar,SportsDirect.Controls.LiveScoresControls.ashx?_method=changeDay&_session=no'

        data = {
            'league': '1',
            'SeasonString': '2012-2013',
            'Year': '1',
            'Month': '1',
            'Day': '1'
        }        

        # data = urllib.parse.urlencode(data).encode('utf-8')
        data = json.dumps(data).encode('utf-8')

        headers = {
            'Host': 'scores.covers.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'Accept-Encoding': 'gzip, deflate',
            'Referer': 'http://scores.covers.com/football-scores-matchups.aspx',
            'Content-Type': 'application/json',
            'Connection': 'keep-alive',
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache'
        }

        req = urllib.request.Request(url)
        req.data = data
        req.headers = headers

        f = urllib.request.urlopen(req)
        print(f.read())

        return f

这给出了:

In [1]: import scraper

In [2]: s = scraper.Scraper()

In [3]: s.scrape()
b"new Object();r.error = new ajax_error('System.ArgumentException','Object of type \\'System.DBNull\\' cannot be convert
ed to type \\'System.String\\'.',0)"
Out[3]: <http.client.HTTPResponse at 0x4d6b650>

提前致谢。

【问题讨论】:

    标签: python ajax json urllib


    【解决方案1】:

    使用data="league=1\nSeasonString=2014-2015\nYear=1\nMonth=1\nDay=1"

    数据不是json类型。

    【讨论】:

    • 谢谢!这正是问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 2012-07-23
    • 1970-01-01
    • 2019-03-16
    • 2021-06-11
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多