我正在尝试检索这些结果的第二页:
http://raceresults.sportstats.ca/display-results.xhtml?raceid=451
如果我点击底部的第 2 页,它会转到第 2 页,但 URL 保持不变。如果我查看 http header ,我可以看到这个 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)
但这仍然只是返回第一页的结果。我这样做是对的吗?我有什么想法可以得到第二页的结果吗?
请您参考如下方法:
您最好的选择可能是使用 Selenium 和相应的 python 包。 Selenium 允许您使用 python 打开并自动控制网络浏览器。这将允许您与他们的下一页按钮进行交互,并在 python 脚本中读取结果。
