在Python中发送带有片段标识符的GET请求

gam*_*ast 4 html python url get

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

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

但是,我总是最终得到基本页面(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')
Run Code Online (Sandbox Code Playgroud)

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

TkT*_*ech 5

锚(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
Run Code Online (Sandbox Code Playgroud)

注意sort_column论点吗?您可以更改此值来确定结果的顺序。使用lxml之类的库来解析results_html字段并保存,即可完成。