【问题标题】:Python - Handling a javascript URL?Python - 处理 javascript URL?
【发布时间】:2012-08-15 01:37:38
【问题描述】:

我正在尝试下载通过 javascript 请求的页面的 html,通常是通过单击浏览器中的链接。我可以下载第一页,因为它有一个通用 URL:

http://www.locationary.com/stats/hotzone.jsp?hz=1

但是页面底部的链接是数字(1 到 10)。因此,如果您单击一个,它将转到例如第 2 页:

http://www.locationary.com/stats/hotzone.jsp?ACTION_TOKEN=hotzone_jsp$JspView$NumericAction&inPageNumber=2

当我将该 URL 放入我的程序并尝试下载 html 时,它给了我网站上不同页面的 html,我认为它是主页。

如果没有特定的URL,如何获取这个使用javascript的URL的html?

谢谢。

附:我正在使用 urllib/urllib2 和 cookielib。

另外,我刚刚发现了一个叫做 PyQuery 的东西?我可以用那个吗?我该怎么做?

代码:

import urllib
import urllib2
import cookielib
import re

URL = ''

def load(url):

    data = urllib.urlencode({"inUserName":"email", "inUserPass":"password"})
    jar = cookielib.FileCookieJar("cookies")
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
    opener.addheaders.append(('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1'))
    opener.addheaders.append(('Referer', 'http://www.locationary.com/'))
    opener.addheaders.append(('Cookie','site_version=REGULAR'))
    request = urllib2.Request("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction", data)
    response = opener.open(request)
    page = opener.open("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction").read()

    h = response.info().headers
    jsid = re.findall(r'Set-Cookie: (.*);', str(h[5]))
    data = urllib.urlencode({"inUserName":"email", "inUserPass":"password"})
    jar = cookielib.FileCookieJar("cookies")
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
    opener.addheaders.append(('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1'))
    opener.addheaders.append(('Referer', 'http://www.locationary.com/'))
    opener.addheaders.append(('Cookie','site_version=REGULAR; ' + str(jsid[0])))
    request = urllib2.Request("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction", data)
    response = opener.open(request)
    page = opener.open(url).read()
    print page

load(URL)

【问题讨论】:

  • 你能展示一些你正在使用的代码吗?

标签: javascript python html pyquery


【解决方案1】:

我自己没用过,但是听说过Requests的好东西。

【讨论】:

    【解决方案2】:

    查看项目Splinter,也许有用:http://splinter.cobrateam.info/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      相关资源
      最近更新 更多