【问题标题】:Cannot download html (entire web page)无法下载 html(整个网页)
【发布时间】:2018-04-10 18:21:42
【问题描述】:

我正在尝试从

下载整个 html 代码

http://www.ivolatility.com/options/AMZN/NASDAQ/

输出不包括表中的数据。

这是我正在使用的代码

url = 'http://www.ivolatility.com/options/AMZN/NASDAQ/'
r = requests.get(url, allow_redirects=True)
open('C:.../Downloads/amzn.html', 'wb').write(r.content)

我认为这可能与注册问题有关。

我能做什么?

谢谢

【问题讨论】:

    标签: python-2.7 web-scraping python-requests


    【解决方案1】:

    您的请求会返回一个登录表单,这意味着您必须登录才能访问数据。

    登录过程相对简单——我们只需将表单数据提交到登录页面(并使用session 对象来存储cookies)。
    然后我们可以使用经过身份验证的会话来检索表内容。

    代码,

    import requests
    
    url = 'http://www.ivolatility.com/options/AMZN/NASDAQ/'
    login_url = 'https://www.ivolatility.com/login.j'
    usr = 'my username'
    pwd = 'my password'
    data = {
        'username':usr, 'password':pwd,
        'ref_url':login_url, 'service_name':'Home Page', 
        'step':1, 'login__is__sent':1
        }
    
    s = requests.session()
    s.post(login_url, data)
    r = s.get(url)
    
    with open('my file', 'wb') as f:
        f.write(r.content)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-23
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2018-01-29
      相关资源
      最近更新 更多