requests模拟登陆

第一种方式,用户名密码登陆:

#!/usr/bin/python3
#-*- coding:utf-8 -*-

import requests
import html5lib
import re
from bs4 import BeautifulSoup

s = requests.Session()

url_login = 'http://accounts.douban.com/login'
url_contacts = 'https://www.douban.com/people/163532496/'

formdata = {
    'redir':'https://www.douban.com',
    'form_email': 'yourUsername',
    'form_password': 'yourPassword',
    'login': '登陆'
}

headers = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
           "Accept-Encoding":"gzip, deflate, sdch, br",
           "Accept-Language":"en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4",
           "cache-control":"max-age=0",
           "Referer":"https://www.douban.com/",
           "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
           }

r = s.post(url_login, data = formdata, headers = headers)
content = r.text
soup = BeautifulSoup(content, 'html5lib')
captcha = soup.find('img', id = 'captcha_image')
if captcha:
    captcha_url = captcha['src']
    re_captcha_id = r'<input type="hidden" name="captcha-id" value="(.*?)"/'
    captcha_id = re.findall(re_captcha_id, content)
    print (captcha_id)
    print (captcha_url) #打印验证码url地址
    captcha_text = input('Please input the captcha:') #手工输入验证码信息
    formdata['captcha-solution'] = captcha_text
    formdata['captcha-id'] = captcha_id
    print(formdata)
    r = s.post(url_login, data = formdata, headers = headers)

r = s.get(url_contacts)

print (r.text)
#with open('contacts.txt', 'w+', encoding='utf-8') as f:
#    f.write(r.text)

如何确定form_email form_password captcha-solution  captcha-id等关键字,需要在浏览器,如chrom中F12打开调试模式,可以在Network中找到login页面,可以找到Form Data


二 使用cookie登陆:

当用户名账号方式登陆成功以后,选择记住密码,关掉网页后重新打开,在调试模式的Network里面可以找到Request Headers

里面可以看到Cookie项,得到cookie值.

#!/usr/bin/python3
#-*- coding:utf-8 -*-
import requests

url = 'http://www.douban.com'
cookies = {'cookie': 'your cookie value'}
headers = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
           "Accept-Encoding":"gzip, deflate, sdch, br",
           "Accept-Language":"en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4",
           "cache-control":"max-age=0",
           "Referer":"https://www.douban.com/",
           "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
           }

r = requests.get(url, cookies = cookies, headers = headers)

获取豆瓣电影的top250:

#!/usr/bin/python3
#-*- coding:utf-8 -*-

import requests
from lxml import etree

s = requests.Session()
for id in range(0, 251, 25):
    url = 'https://movie.douban.com/top250?start=%s&filter=' % str(id)
    r = s.get(url)
    r.encoding = 'utf-8'
    root = etree.HTML(r.content)
    items = root.xpath("//ol/li/div[@class='item']")
    #print(len(items))
    for item in items:
        title = item.xpath('./div[@class="info"]//a/span[1][@class="title"]/text()') #名字
        name = title[0].encode('gb2312', 'ignore').decode('gb2312')
        print (name)
        #score = item.xpath('./div[@class="info"]/div[2]/div/span[2]/text()') #评分
        score = item.xpath('.//div[@class="bd"]//span[@class="rating_num"]/text()')[0]#评分
        print (score[0])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值