繁体   English   中英

如何实现从机械化到pycurl的登录切换

[英]How to implement login handover from mechanize to pycurl

我需要通过在python中使用机械化来登录网站,然后继续使用pycurl遍历该网站。 因此,我需要知道的是如何将通过机械化建立的登录状态转换为pycurl。 我认为这不只是复制cookie。 或者是吗? 代码示例很有价值;)

为什么我不愿意单独使用pycurl:我有时间限制,在修改示例5分钟后,我的机械化代码如下:

import mechanize
import cookielib

# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# debugging messages?
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)

# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# Open the site
r = br.open('https://thewebsite.com')
html = r.read()

# Show the source
print html
# or
print br.response().read()

# Show the html title
print br.title()

# Show the response headers
print r.info()
# or
print br.response().info()

# Show the available forms
for f in br.forms():
    print f

# Select the first (index zero) form
br.select_form(nr=0)

# Let's search
br.form['username']='someusername'
br.form['password']='somepwd'
br.submit()

print br.response().read()

# Looking at some results in link format
for l in br.links(url_regex='\.com'):
    print l

现在,如果我只能将正确的信息从br对象传输到pycurl,那么我将完成。

为什么我不愿意单独使用机械化:机械化基于urllib,而urllib是一场噩梦。 我有太多令人不安的问题。 我可以吞下一两个电话才能登录,但是请不要再打扰。 相比之下,pycurl对我而言是稳定,可定制和快速的。 根据我的经验,从pycurl到urllib就像从星际迷航到打火石。

PS:万一有人想知道,一旦有了HTML,我就会使用BeautifulSoup

解决了它。 显然,它与cookie有关。 这是我获取Cookie的代码:

import cookielib
import mechanize

def getNewLoginCookieFromSomeWebsite(username = 'someusername', pwd = 'somepwd'):
    """
    returns a login cookie for somewebsite.com by using mechanize
    """
    # Browser
    br = mechanize.Browser()

    # Cookie Jar
    cj = cookielib.LWPCookieJar()
    br.set_cookiejar(cj)

    # Browser options
    br.set_handle_equiv(True)
    br.set_handle_gzip(True)
    br.set_handle_redirect(True)
    br.set_handle_referer(True)
    br.set_handle_robots(False)

    # Follows refresh 0 but does not hang on refresh > 0
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

    # User-Agent
    br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) Gecko/20100101 Firefox/26.0')]

    # Open login site
    response = br.open('https://www.somewebsite.com')

    # Select the first (index zero) form
    br.select_form(nr=0)

    # Enter credentials
    br.form['user']=username
    br.form['password']=pwd
    br.submit()

    cookiestr = ""
    for c in br._ua_handlers['_cookies'].cookiejar:
        cookiestr+=c.name+'='+c.value+';'

    return cookiestr

为了在使用pycurl时激活该cookie的用法,您要做的就是在c.perform()出现之前键入以下内容:

c.setopt(pycurl.COOKIE, getNewLoginCookieFromSomeWebsite("username", "pwd"))

请注意:某些网站可能会继续通过Set-Content与Cookie进行交互,而pycurl(与机械化不同)不会自动对Cookie执行任何操作。 Pycurl只是接收该字符串,并留给用户该怎么做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM