我需要通过在 python 中使用 mechanize 登录到一个网站,然后使用 pycurl 继续遍历该网站。所以我需要知道的是如何将通过 Mechanize 建立的登录状态转移到 pycurl 中。我认为这不仅仅是复制 cookie。或者是吗?代码示例很有值(value);)
为什么我不愿意单独使用pycurl:
我有时间限制,我的 Mechanize 代码在修改 5 分钟后工作 this示例如下:
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,我就大功告成了。
为什么我不愿意单独使用 Mechanize :
Mechanize 是基于 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(与 Mechanize 不同)不会自动对 cookie 执行任何操作。 Pycurl 只是简单地接收字符串并让用户如何处理它。
关于python - 如何实现从 Mechanize 到pycurl的登录切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28400490/