首先,我认为值得一提的是,我知道有很多类似的问题,但没有一个对我有用。。。
我是Python、html和web scraper的新手。我试图从一个需要先登录的网站上获取用户信息。在我的测试中,我使用来自github的scraper电子邮件设置作为示例。主页面是“https://github.com/login”,目标页是“https://github.com/settings/emails”
以下是我尝试过的方法
##################################### Method 1
import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup
import html2text
br = mechanize.Browser()
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)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Chrome')]
# The site we will navigate into, handling it's session
br.open('https://github.com/login')
for f in br.forms():
print f
br.select_form(nr=0)
# User credentials
br.form['login'] = 'myusername'
br.form['password'] = 'mypwd'
# Login
br.submit()
br.open('github.com/settings/emails').read()
################ Method 2
import urllib, urllib2, cookielib
username = 'myusername'
password = 'mypwd'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'j_password' : password})
opener.open('https://github.com/login', login_data)
resp = opener.open('https://github.com/settings/emails')
print resp.read()
############# Method 3
import urllib
opener = urllib.FancyURLopener()
print opener.open('http://myusername:mypwd@github.com/settings/emails').read()
########## Method 4
import mechanize
import cookielib
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
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)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
br.addheaders = [('User-agent', 'Chrome')]
br.add_password('https://github.com/settings/emails', 'myusername', 'mypwd')
br.open('https://github.com/settings/emails')
print br.response().read()
############ Methods 5
from requests import session
payload = {
'action': 'login',
'username': 'myusername',
'password': 'mypwd'
}
with session() as c:
c.post('https://github.com/login', data=payload)
request = c.get('https://github.com/settings/emails')
print request.headers
print request.text
########### Method 6
import requests
from requests.packages.urllib3 import add_stderr_logger
import sys
from bs4 import BeautifulSoup as bs
add_stderr_logger()
s = requests.Session()
s.headers['User-Agent'] = 'Chrome'
username = 'myusername'
password = 'mypwd'
url = 'https://github.com/login'
# after examining the HTML of the website you're trying to log into
# set name_form to the name of the form element that contains the name and
# set password_form to the name of the form element that will contain the password
login = {'login': username, 'password': password}
login_response = s.post(url, data=login)
for r in login_response.history:
if r.status_code == 401: # 401 means authentication failed
print 'error!'
sys.exit(1) # abort
pdf_response = s.get('https://github.com/settings/emails') # Your cookies and headers are automatically included
soup = bs(pdf_response.content)
此外,我还阅读了一些关于HTTP身份验证和cookies之间差异的讨论。但他们都没起作用。
请帮忙,如有任何帮助将不胜感激。非常感谢你。
解决这个问题的经典方法是:
作为自动化的爱好者,我们认为我们可以编写一些代码来自动化第2点(这实际上是最耗时的),结果是login form,一个库,它可以自动填写登录表单,给出登录页面、用户名和密码。 下面是一个简单的蜘蛛的代码,它将使用loginform自动登录到站点。
githubloginspider.py公司
项目.py
loginform.py文件
我想在旁边加上我的解决方案。这个答案主要遵循我在做任何事情时都遵循的老套/懒惰的方法。主要是因为,我懒得处理cookies、会话数据等。
如果您想在使用单个帐户凭据(如所有pinterest板)登录后刮掉网站的多个页面,则此解决方案最有用。如果您想使用多个帐户自动进行身份验证,则此解决方案不可用
所以我的解决方案是selenium和firefox配置文件。
我设计了这个机制,当我遇到需要刮掉一些pinterest页面时,我从示例中添加了几行代码来说明如何使用概要文件。根据你的需要来调整代码。
这对我有用:
你一点也不远!
相关问题 更多 >
编程相关推荐