【问题标题】:Sending GET Request with fragment identifier in Python在 Python 中发送带有片段标识符的 GET 请求
【发布时间】:2014-11-19 19:02:49
【问题描述】:

我正在尝试使用包含片段标识符的请求模块发送 GET 请求。我有以下代码:

url = 'http://steamcommunity.com/market/search?appid=730#p20_quantity_desc'
page = requests.get(url, headers=headers)

但是,我最终总是得到基本页面 (http://steamcommunity.com/market/search?appid=730),而不是带有片段标识符的页面(似乎没有发送 #p20_quanitity_description)。

urllib2 也不适用于我的代码:

req = urllib2.Request(url, headers={ 'User-Agent': 'Mozilla/5.0' })
page = urllib2.urlopen(req).read().decode('UTF-8', 'ignore')

我将如何发送 GET 请求并在 URL 中包含 #p20_quanitity_description?

【问题讨论】:

    标签: python html url get


    【解决方案1】:

    锚点 (p20_quantity_desc) 对服务器没有任何意义。页面上有一些 Javascript 会根据此锚点更改结果的排序顺序,但这是客户端。 Requests/urllib 将看到相同的页面响应,无论是否有锚。

    尝试在页面上禁用 Javascript,您就会明白我的意思了。

    您想要做的是向页面正在使用的 API 端点发出请求。这是一个例子:

    http://steamcommunity.com/market/search/render/?query=&start=0&count=10&search_descriptions=0&sort_column=quantity&sort_dir=asc&appid=730
    

    注意sort_column 参数?这是您可以更改以决定结果排序的值。使用 lxml 之类的库来解析 results_html 字段,然后就完成了。

    【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多