如何使用Python从需要登录信息的网站下载文件?

如何使用Python从需要登录信息的网站下载文件?,python,html,login,web,urllib2,Python,Html,Login,Web,Urllib2,我正在尝试使用Python从网站下载一些数据。如果您只是复制并粘贴url,除非您填写登录信息,否则它不会显示任何内容。我有登录名和密码,但是我应该如何在Python中包含它们 我目前的代码是: import urllib, urllib2, cookielib username = my_user_name password = my_pwd link = 'www.google.com' # just for instance cj = cookielib.CookieJar() open

我正在尝试使用Python从网站下载一些数据。如果您只是复制并粘贴url,除非您填写登录信息,否则它不会显示任何内容。我有登录名和密码,但是我应该如何在Python中包含它们

我目前的代码是:

import urllib, urllib2, cookielib

username = my_user_name
password = my_pwd

link = 'www.google.com' # just for instance
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'j_password' : password})

opener.open(link, login_data)
resp = opener.open(link,login_data)
print resp.read()
没有弹出错误,但是resp.read()是一堆CSS,它只有“在阅读这里的新闻之前必须登录”这样的消息

那么,登录后如何检索该页面? 刚刚注意到该网站需要3个条目:

Company: 

Username: 

Password:
我拥有所有这些,但是如何将这三个都放在login变量中呢

如果我在没有登录的情况下运行它,它将返回:

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

opener.open(dd)
resp = opener.open(dd)

print resp.read()
以下是打印件:

<DIV id=header>
<DIV id=strapline><!-- login_display -->
<P><FONT color=#000000>All third party users of this website and/or data produced by the Baltic do so at their own risk. The Baltic owes no duty of care or any other obligation to any party other than the contractual obligations which it owes to its direct contractual partners. </FONT></P><IMG src="images/top-strap.gif"> <!-- template [strapline]--></DIV><!-- end strapline -->
<DIV id=memberNav>
<FORM class=members id=form1 name=form1 action=client_login/client_authorise.asp?action=login method=post onsubmits="return check()">

本网站的所有第三方用户和/或波罗的海生产的数据均自行承担风险。波罗的海对任何一方都不负有注意义务或任何其他义务,但对其直接合同伙伴负有的合同义务除外


使用scrapy对数据进行爬网

然后你就可以这么做了

class LoginSpider(Spider):
    name = 'example.com'
    start_urls = ['http://www.example.com/users/login.php']

    def parse(self, response):
        return [FormRequest.from_response(response,
                    formdata={'username': 'john', 'password': 'secret'},
                    callback=self.after_login)]

    def after_login(self, response):
        # check login succeed before going on
        if "authentication failed" in response.body:
            self.log("Login failed", level=log.ERROR)
            return

此代码应该可以工作,只需使用实际域和登录数据替换

from requests import Session

s = Session() # this session will hold the cookies

# here we first login and get our session cookie
s.post("http://.../client_login/client_authorise.asp?action=login", {"companyName":"some_company", "password":"some_password", "username":"some_user", "status":""})

# now we're logged in and can request any page
resp = s.get("http://.../").text

print(resp)

它不起作用,print resp.read()仍然返回“仅通过订阅访问此数据。免费试用。

”@André我注意到该页面需要3项登录,我已经全部拥有,但我不确定如何将其放入登录信息中?我已经编辑过,但不确定这是否是您要求的。我在print resp.read()结果中没有找到..这可能有用,但我认为他不需要这么大的库来完成登录之类的琐碎任务。。。在Python请求甚至urllib的两行代码中的一行也可以做到这一点。我现在没有scrapy,我必须要求它为我安装它,因为Python在服务器上。谢谢,但在resp变量中,我仍然有“>访问此数据的权限,仅限订阅。免费试用。

”。。。。。我确信登录名是正确的