【发布时间】:2015-03-05 23:09:28
【问题描述】:
我正在尝试检索这些结果的第二页:
http://raceresults.sportstats.ca/display-results.xhtml?raceid=451
如果我点击底部的第 2 页,它会转到第 2 页,但 URL 保持不变。如果我查看 http 标头,我可以看到这个 cookie:
Set-Cookie: sportstats_preferences="{\"raceId\":451,\"firstRow\":40,
\"category\":\"All Categories\",\"chronosStep\":\"INSTRUCTIONS
\",\"facebookLoggedIn\":false,\"twitterLoggedIn\":false,\"fbServiceId
\":0,\"twServiceId\":0,\"unit\":1}"; Version=1; Max-Age=2592000;
Expires=Sat, 04-Apr-2015 14:30:28 GMT
我可以看到这与第一页不同,firstRow 被设置为 40。
我正在尝试使用以下代码在 Python 3 中获取此第二页:
#!/usr/bin/env python
import urllib.request
opener = urllib.request.build_opener()
cookie = 'sportstats_preferences="{{\\"raceId\\":451,\\"firstRow\\":40,\\"category\\":\\"All Categories\\",\\"chronosStep\\":\\"INSTRUCTIONS\\",\\"facebookLoggedIn\\":false,\\"twitterLoggedIn\\":false,\\"fbServiceId\\":0,\\"twServiceId\\":0,\\"unit\\":1}}"; Version=1; Max-Age=2592000; Expires=Sat, 04-Apr-2015 04:18:36 GMT'
opener.addheaders = [('Cookie', cookie)]
f = opener.open(url).read().decode("utf-8")
for line in f.splitlines():
print(line)
但这仍然只是返回第一页的结果。我会以正确的方式解决这个问题吗?有什么想法可以让我获得第二页的结果吗?
【问题讨论】:
-
我还注意到,如果我点击第二页并查看源代码,我仍然会看到第一页的数据。但是,如果我刷新页面,它将显示源中的第 2 页结果。我尝试在我的代码中打开 URL 两次,但没有帮助。
-
它们在页面中有一个视图状态(在
<input type="hidden" name="javax.faces.ViewState"...>,请参阅页面源代码),我认为服务器使用它来决定显示哪个页面。
标签: python python-3.x cookies urllib