python3爬虫开发实践崔庆才——练习模拟登陆爬取Github

#coding:utf-8

import requests
from lxml import etree

class Login(object):

 def __init__(self):
 
    self.headers = {
        'Referer': 'https://github.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        'Host': 'github.com'
    }
    self.login_url = 'https://github.com/login'
    self.post_url = 'https://github.com/session'
    self.logined_url = 'https://github.com/settings/profile'
    # requests 库的Session ,它可以帮助我们维持一个会话;自动处理cookies;
    self.session = requests.Session()   

def token(self):
"""
获取初始页面的cookies;获取到authenticity_token;
"""
    response = self.session.get(self.login_url, headers=self.headers)
    selector = etree.HTML(response.text)
    token = selector.xpath('//div//input[2]/@value')
    return token

def login(self, email, password):
    post_data = {
        'commit': 'Sign in',
        'utf8': '✓',
        'authenticity_token': self.token()[0],
        'login': email,
        'password': password
    }
    response = self.session.post(self.post_url, data=post_data, headers=self.headers)
    if response.status_code == 200:
        self.dynamics(response.text)

    response = self.session.get(self.logined_url, headers=self.headers)
    if response.status_code == 200:
        # print (response.text)
        self.profile(response.text)

def dynamics(self, html):
    """
    提取了所有的动态信息,然后将其遍历输出。
    """
    selector = etree.HTML(html)
    dynamics = selector.xpath('//div[contains(@class, "news")]//div[contains(@class, "alert")]')
    for item in dynamics:
        dynamic = ' '.join(item.xpath('.//div[@class="title"]//text()')).strip()
        # print("dynamic: ", dynamic)

def profile(self, html):
    """
    来处理个人详’情页信息;提取了个人的昵称和绑定的邮箱,然后将其输出
    """
    selector = etree.HTML(html)
    # print (selector.xpath('//input[@id="user_profile_name"]/@value'))
    name = selector.xpath('//input[@id="user_profile_name"]/@value')
    email = selector.xpath('//select[@id="user_profile_email"]/option[@value!=""]/text()')
    print(name, email)

if name == “main”:
login = Login()
login.login(email=‘xxxx.com’, password=‘xxxxxx’)

python3爬虫开发实践崔庆才——练习模拟登陆爬取Github_第1张图片

[‘yang0103’] [‘[email protected]’]

你可能感兴趣的:(python_爬虫,python_模拟登陆)