• Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A project to attempt to automatically login to a website given a single seed

Autologin: Automatic login for web spiders

PyPI Version Build Status Code Coverage

Autologin is a library that makes it easier for web spiders to crawl websites that require login. Provide it with credentials and a URL or the html source of a page (normally the homepage), and it will attempt to login for you. Cookies are returned to be used by your spider.

The goal of Autologin is to make it easier for web spiders to crawl websites that require authentication without having to re-write login code for each website.

Autologin can be used as a library, on the command line, or as a service. You can make use of Autologin without generating http requests, so you can drop it right into your spider without worrying about impacting rate limits.

If you are using Scrapy for crawling, check out autologin-middleware, which is a scrapy middleware that uses autologin http-api to maintain a logged-in state for a scrapy spider.

Autologin works on Python 2.7 and 3.3+.

Note

The library is in the alpha stage. API can still change, especially around the keychain UI.

Features

  • Automatically find login forms and fields
  • Obtain authenticated cookies
  • Obtain form requests to submit from your own spider
  • Extract links to login pages
  • Use as a library with or without making http requests
  • Command line client
  • Web service
  • UI for managing login credentials
  • Captcha support

Quickstart

Don't like reading documentation?

from autologin import AutoLogin

url = 'https://reddit.com'
username = 'foo'
password = 'bar'
al = AutoLogin()
cookies = al.auth_cookies_from_url(url, username, password)

You now have a cookiejar that you can use in your spider. Don't want a cookiejar?

cookies.__dict__

You now have a dictionary.

Installation

Install the latest release from PyPI:

$ pip install -U autologin

or the version with the latest changes from Github:

$ pip install git+https://github.com/TeamHG-Memex/autologin.git

Autologin depends on Formasaurus for field and form classification, which has quite a lot of dependencies. These packages may require extra steps to install, so the command above may fail. In this case install dependencies manually, one by one (follow their install instructions).

A recent pip is recommended (update it with pip install pip -U). On Ubuntu, the following packages are required:

$ apt-get install build-essential libssl-dev libffi-dev \
                  libxml2-dev libxslt1-dev \
                  python-dev  # or python3-dev for python 3

If you want to just use the HTTP API, another option is to use a docker image:

docker pull hyperiongray/autologin
docker run -p 8088:8088 -p 8089:8089 hyperiongray/autologin

Keychain UI stores credentials in an sqlite database that is stored in the /var/autologin volume. One way of preserving the credentials is to mount a host directory (current directory in this example) as the data volume:

docker run -p 8088:8088 -p 8089:8089 -v `pwd`:/var/autologin/ \
    hyperiongray/autologin

A db.sqlite file will be created in the specified directory. The keychain UI will be accessible at http://127.0.0.1:8088 for Linux, and at the ip address of the Docker machine for OS X and Windows (for example, http://192.168.99.100:8088).

Auth cookies from URL

This method makes an http request to the URL, extracts the login form (if there is one), fills the fields and submits the form. It then return any cookies it has picked up:

cookies = al.auth_cookies_from_url(url, username, password)

Note that it returns all cookies, they may be session cookies rather than authenticated cookies.

This call is blocking, and uses Crochet to run the Twisted reactor and a Scrapy spider in a separate thread. If you have a Scrapy spider (or use Twisted in some other way), use the HTTP API, or the non-blocking API (it's not documented, see http_api.AutologinAPI._login).

There are also optional arguments for AutoLogin.auth_cookies_from_url:

  • settings is a dictionary with Scrapy settings to override. Useful settings to pass include:

    • HTTP_PROXY, HTTPS_PROXY set proxies to use for all requests.
    • SPLASH_URL if set, Splash will be used to make all requests. Use it if your crawler also uses splash and the session is tied to IP and User-Agent, or for Tor sites.
    • USER_AGENT overrides default User-Agent.
  • extra_js (experimental) is a string with an extra JS script that should be executed on the login page before making a POST request. For example, it can be used to accept cookie use. It is supported only when SPLASH_URL is also given in settings.

An example of using this options:

cookies = al.auth_cookies_from_url(
    url, username, password,
    extra_js='document.getElementById("accept-cookies").click();',
    settings={
        'SPLASH_URL': 'http://127.0.0.1:8050',
        'USER_AGENT': 'Mozilla/2.02 [fr] (WinNT; I)',
    })

Login request

This method extracts the login form (if there is one), fills the fields and returns a dictionary with the form url and args for your spider to submit. No http requests are made:

>>> al.login_request(html_source, username, password, base_url=None)
{'body': 'login=admin&password=secret',
 'headers': {b'Content-Type': b'application/x-www-form-urlencoded'},
 'method': 'POST',
 'url': '/login'}

Relative form action will be resolved against the base_url.

Command Line

$ autologin
usage: autologin [-h] [--splash-url SPLASH_URL] [--http-proxy HTTP_PROXY]
                 [--https-proxy HTTPS_PROXY] [--extra-js EXTRA_JS]
                 [--show-in-browser]
                 username password url

HTTP API

You can start the autologin HTTP API with:

$ autologin-http-api

and use /login-cookies endpoint. Make a POST request with JSON body. The following arguments are supported:

  • url (required): url of the site where we would like to login
  • username (optional): if not provided, it will be fetched from the login keychain
  • password (optional): if not provided, it will be fetched from the login keychain
  • extra_js (optional, experimental) is a string with an extra JS script that should be executed on the login page before making a POST request. For example, it can be used to accept cookie use. It is supported only when SPLASH_URL is also given in settings.
  • settings (optional) - a dictionary with Scrapy settings to override, useful values are described above.

If username and password are not provided, autologin tries to find them in the login keychain. If no matching credentials are found (they are matched by domain, not by precise url), then human is expected to eventually provide them in the keychain UI, or mark domain as "skipped".

Response is JSON with the following fields:

  • status, which can take the following values:

    • error status means an error occurred, error field has more info
    • skipped means that domain is marked as "skipped" in keychain UI
    • pending means there is an item in keychain UI (or it was just created), and no credentials have been entered yet
    • solved means that login was successful and cookies were obtained
  • error - human-readable explanation of the error.

  • response - last response received by autologin (can be None in some cases). This is a dict with cookies, headers, and either a text or body_b64 fields (depending on response content type).

  • cookies - a list of dictionaries in Cookie.__dict__ format. Present only if status is solved.

  • start_url - a url that was reached after successful login.

Proxy support

Proxies can be specified via HTTP_PROXY and HTTPS_PROOXY keys in settings argument. Username and password can be specified as part of the proxy url (the format is protocol://username:password@url).

If you are using proxy with Splash, it is assumed that you want to have Splash make requests via given proxy, and not make a request to Splash via proxy. HTTP_PROXY is always used for Splash.

Captcha support

There is experimental captcha support: if the login form contains a captcha, we will try to solve it using an external service (DeathByCaptcha), and will submit it as part of login request. This does not affect API in any way, you only have to provide environment variables with your DeathByCaptcha account details: DEATHBYCAPTCHA_USERNAME and DEATHBYCAPTCHA_PASSWORD. This applies to all APIs: autologin-http-api, autologin, and the Python API.

You also need to install the decaptcha library:

pip install git+https://github.com/TeamHG-Memex/decaptcha.git

Support is still experimental, new Google ReCaptcha/NoCaptcha are not supported. Also, it currently works only with splash (when SPLASH_URL is passed in settings).

Keychain UI

Start keychain UI with:

$ autologin-server

Note that both autologin-server and autologin-http-api are not protected by any authentication.

Keychain UI stores credentials in an sqlite database. It is located near the library itself by default, which is not always good, especially if you want to persist the data between updates or do not have write permissions for that folder. You can configure database location and secret_key used by the flask app by creating an /etc/autologin.cfg or ~/.autologin.cfg file (should be the same user under which autologin services are running). Here is an example config that changes default secret_key and specifies a different database path (both items are optional):

[autologin]
secret_key = 8a0b923820dcc509a6f75849b
db = /var/autologin/db.sqlite

Contributors

Source code and bug tracker are on github: https://github.com/TeamHG-Memex/autologin.

Run tests with tox:

$ tox

Splash support is not tested directly here, but there are indirect tests for it in the autologin-middleware test suite.

License

License is MIT.


define hyperiongray

More Repositories

1

eli5

A library for debugging/inspecting machine learning classifiers and explaining their predictions
Jupyter Notebook
2,758
star
2

scrapy-rotating-proxies

use multiple proxies with Scrapy
Python
656
star
3

tensorboard_logger

Log TensorBoard events without touching TensorFlow
Python
625
star
4

sklearn-crfsuite

scikit-learn inspired API for CRFsuite
Python
421
star
5

aquarium

Splash + HAProxy + Docker Compose
Python
192
star
6

deep-deep

Adaptive crawler which uses Reinforcement Learning methods
Jupyter Notebook
165
star
7

arachnado

Web Crawling UI and HTTP API, based on Scrapy and Tornado
Python
156
star
8

html-text

Extract text from HTML
HTML
115
star
9

Formasaurus

Formasaurus tells you the type of an HTML form and its fields using machine learning
HTML
110
star
10

page-compare

Simple heuristic for measuring web page similarity (& data set)
HTML
88
star
11

autopager

Detect and classify pagination links
HTML
86
star
12

undercrawler

A generic crawler
Python
75
star
13

scrapy-crawl-once

Scrapy middleware which allows to crawl only new content
Python
74
star
14

soft404

A classifier for detecting soft 404 pages
Jupyter Notebook
53
star
15

agnostic

Agnostic Database Migrations
Python
51
star
16

autologin-middleware

Scrapy middleware for the autologin
Python
37
star
17

json-lines

Read JSON lines (jl) files, including gzipped and broken
Python
34
star
18

extract-html-diff

extract difference between two html pages
HTML
29
star
19

scrapy-kafka-export

Scrapy extension which writes crawled items to Kafka
Python
28
star
20

MaybeDont

A component that tries to avoid downloading duplicate content
Python
27
star
21

sitehound-frontend

Site Hound (previously THH) is a Domain Discovery Tool
HTML
23
star
22

imageSimilarity

Given a new image, determine if it is likely derived from a known image.
Python
20
star
23

domain-discovery-crawler

Broad crawler for domain discovery
Python
17
star
24

url-summary

Show summary of a large number of URLs in a Jupyter Notebook
Python
17
star
25

sitehound

This is the facade for installation and access to the individual components
Shell
16
star
26

tor-proxy

a tor socks proxy docker image
11
star
27

scrapy-dockerhub

[UNMAINTAINED] Deploy, run and monitor your Scrapy spiders.
Python
10
star
28

web-page-annotator

Annotate parts of web pages in the browser
Python
9
star
29

scrash-lua-examples

A collection of example LUA scripts and JS utilities
JavaScript
7
star
30

scrapy-cdr

Item definition and utils for storing items in CDR format for scrapy
Python
7
star
31

hh-page-classifier

Headless Horseman Page Classifier service
Python
6
star
32

privoxy

Privoxy HTTP Proxy based on jess/privoxy
6
star
33

sitehound-backend

Sitehound's backend
HTML
6
star
34

fortia

[UNMAINTAINED] Firefox addon for Scrapely
JavaScript
5
star
35

proxy-middleware

Scrapy middleware that reads proxy config from settings
Python
5
star
36

linkrot

[UNMAINTAINED] A script (Scrapy spider) to check a list of URLs.
Jupyter Notebook
4
star
37

hgprofiler

JavaScript
4
star
38

linkdepth

[UNMAINTAINED] scrapy spider to check link depth over time
Python
4
star
39

common-crawl-mapreduce

A naive scoring of commoncrawl's content using MR
Java
3
star
40

captcha-broad-crawl

Broad crawl of onion sites in search for captchas
Python
3
star
41

frontera-crawler

Crawler-specific logic for Frontera
Python
3
star
42

hh-deep-deep

THH ↔ deep-deep integration
Python
3
star
43

scrapy-login

[UNMAINTAINED] A middleware that provides continuous site login facility
Python
3
star
44

bk-string

A BK Tree based approach to storing and querying strings by Levenshtein Distance.
C
3
star
45

domainSpider

Simple web crawler that sticks to a set list of domains. Work in progress.
Python
3
star
46

quickpin

New iteration of QuickPin with Flask & AngularDart
Python
2
star
47

py-bkstring

A python wrapper for the bk-string C project.
Python
2
star
48

broadcrawl

Middleware that limits number of internal/external links during broad crawl
Python
2
star
49

sshadduser

A simple tool to add a new user with OpenSSH keys.
Python
2
star
50

autoregister

Python
2
star
51

quickpin-api

Python wrapper for the QuickPin API
Python
1
star
52

muricanize

A translation API
Python
1
star
53

rs-bkstring

Rust
1
star
54

scrash-pageuploader

[UNMAINTAINED] S3 Uploader pipelines for HTML and screenshots rendered by Splash
Python
1
star
55

site-checker

JavaScript
1
star
56

frontera-scripts

A set of scripts to spin up EC2 Frontera cluster with spiders
Python
1
star