windows10でIIS

コントロールパネル → プログラム → Windowsの機能の有効化または無効化 → インターネットインフォメーションサービス で有効にする。

seleniumをインストール

[vagrant@localhost python]$ pip3 install selenium
Collecting selenium
Downloading https://files.pythonhosted.org/packages/41/c6/78a9a0d0150dbf43095c6f422fdf6f948e18453c5ebbf92384175b372ca2/selenium-3.13.0-py2.py3-none-any.whl (946kB)
100% |████████████████████████████████| 952kB 552kB/s
Installing collected packages: selenium
Successfully installed selenium-3.13.0

R社にpythonからログイン

まさか、できるわけないよね。

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

USER = "hoge"
PASS = "hogehoge"

session = requests.session()

login_info = {
	"u" : USER,
	"p": PASS,
}
url_login = "https://grp01.id.rakuten.co.jp/rms/nid/vc?__event=login&service_id=top"
res = session.post(url_login, data=login_info)
res.raise_for_status()
print("success")

おい、ちょっと待て、ちょっと待て。マジこれ?これ、やばくないか。。。
[vagrant@localhost python]$ python3 app.py
success

bs4でログイン後の情報をスクレイピングする

まず、login.phpで、sessionのusernameとpasswordがokなら、メールトップ画面にリダイレクトする処理を書いています。

<?php elseif($status == "ok"): header('Location: mail.php?path=u0'); ?> 

こちらがログイン後の画面。outlookのUIを参考にしています。

「yumeさんのメールボックス」がh2です。

続いて、python。 sessionのusernameをyumeでpostして、beautifulsoupでh2をselect_oneします。

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

USER = "yume"
PASS = "hogehoge"

session = requests.session()

login_info = {
	"username" : USER,
	"password": PASS,
}
url_login = "http://open-box.co/login.php"
res = session.post(url_login, data=login_info)
res.raise_for_status()
print("success")

soup = BeautifulSoup(res.text, "html.parser")
a = soup.select_one("h2").string
if a is None:
	print("取得できませんでした")
	quit()
print(a)

[vagrant@localhost python]$ python3 app.py
success
yumeさんのメールボックス

おいおいおい。まてまてまて、頭が追い付かない。
すげー、python!

え、それなら、もしかして、twitterもいける?

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

USER = "hoge"
PASS = "hogehoge"

session = requests.session()

login_info = {
	"signin-email" : USER,
	"signin-password": PASS,
}
url_login = "https://twitter.com/login"
res = session.post(url_login, data=login_info)
res.raise_for_status()
print("success")

[vagrant@localhost python]$ python3 app.py
Traceback (most recent call last):
File “app.py”, line 16, in
res.raise_for_status()
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages/requests/models.py”, line 939, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://twitter.com/login

Forbiddenだ。さすがにあかんか。
twitterに限らず、requests.session()では、ログインできないようにできるらしいですね。
なるほどね。

pythonからusernameとpasswordのsessionをpostしてログインする

よく、不正ログインの防ぐために、画像認証などありますが、どうやってプログラムからログインしてるのか、やっとわかりました。
まず、以前自作したメールボックス
http://open-box.co/login.php

phpで、username, passwordをmysqlと照合してcheckしています。

public function login($username, $password){
		$stmt = $this->mysqli->prepare(
			"SELECT password FROM users WHERE username = ?");
		$stmt->bind_param('s', $_POST["username"]);
		$stmt->execute();

		$stmt->store_result();
		if($stmt->num_rows == 1){
			$stmt->bind_result($hash);
			while ($stmt->fetch()){
				if(password_verify($_POST['password'], $hash)){
					$_SESSION["username"] = $_POST["username"];
					return true;
				}
			}
		}
		return false;
	}

pythonでusernameとpasswordのsessionをpostします。

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

USER = "hoge"
PASS = "hogehoge"

session = requests.session()

login_info = {
	"username" : USER,
	"password": PASS,
}
url_login = "http://open-box.co/login.php"
res = session.post(url_login, data=login_info)
res.raise_for_status()
print("success")

エラーだと、res.raise_for_status()でエラーが表示される筈ですが、、、
来た!!!!!
[vagrant@localhost python]$ python3 app.py
success

bs4も使いたい。

SyntaxError: invalid syntax

しょっぱな躓いた。

from bs4 import BeautifulSoup
from urllib.request import
from urllib.parse import
from os import makedirs
import os.path, time, re

proc_files = {}

def enum_links(html, base):
	soup = BeautifulSoup(html, "html.parser")
	links = soup.select("link[rel='stylesheet']")
	links += soup.select("a[href]")
	result = []
	for a in links:
		href = a.attrs['href']
		url = urljoin(base, href)
		result.append(url)
	return result

def download_file(url):
	o = urlparse(url)
	savepath = "./" + o.netloc + o.path
	if re.search(r"/$", savepath):
		savepath += "index.html"
	savedir = os.path.dirname(savepath)
	if os.path.exists(savepath): return savepath
	if not os.path.exists(savedir):
		print("mkdir=", savedir)
		makedirs(savedir)
	try:
		print("download=", url)
		urlretrive(url, savepath)
		time.sleep(1)
		return savepath
	except:
		print("ダウンロード失敗:", url)
		return None

def analize_html(url, root_url):
	savepath = download_file(url)
	if savepath is None: return
	if savepath in proc_files: return
	proc_files[savepath] = True
	print("analize_html=", url)
	html = open(savepath, "r", encoding="utf-8").read()
	links = enum_links(html, url)
	for link_url in links:
		if link_url.find(root_url) != 0:
			if not re.search(r".css$", link_url): continue
		if re.search(r".(html|html)$", link_url):
			analize_html(link_url, root_url)
			continue
		download_file(link_url)

if __name__ == "__main__":
	url = "http://docs.python.jp/3.5/library"
	analize=html(url, url)

はい?
[vagrant@localhost python]$ python3 app.py
File “app.py”, line 2
from urllib.request import
^
SyntaxError: invalid syntax

≧urllib.request モジュールは基本的な認証、暗号化認証、リダイレクション、Cookie、その他の介在する複雑なアクセス環境において (大抵は HTTP で) URL を開くための関数とクラスを定義します。

from urllib.request importって書き方がおかしい気がするが。。

urllib.parse.urljoin()

base urlの相対パスをurljoinで結合していく。

from urllib.parse import urljoin

base = "http://hoge.com/html/crm.html"

print( urljoin(base, "erp.html"))
print( urljoin(base, "accounting/ifrs.html"))
print( urljoin(base, "../scm.html"))
print( urljoin(base, "../img/bi.png"))
print( urljoin(base, "../css/style.css"))

[vagrant@localhost python]$ python3 app.py
http://hoge.com/html/erp.html
http://hoge.com/html/accounting/ifrs.html
http://hoge.com/scm.html
http://hoge.com/img/bi.png
http://hoge.com/css/style.css

もし、urljoinがhttpの場合は、そのまま絶対パスを返す。

from urllib.parse import urljoin

base = "http://hoge.com/html/crm.html"

print( urljoin(base, "erp.html"))
print( urljoin(base, "http://www.nttdata.com/jp/ja/services/oss/index.html"))
print( urljoin(base, "../scm.html"))

[vagrant@localhost python]$ python3 app.py
http://hoge.com/html/erp.html
http://www.nttdata.com/jp/ja/services/oss/index.html
http://hoge.com/scm.html

なるほど。

find_allと正規表現を組み合わせる

urlにolympicが入っているか、regular expressionで検索します。

from bs4 import BeautifulSoup
import re #正規表現を使用

html = """
<ul>
	<li><a href="https://tokyo2020.org/jp/">東京オリンピック</a></li>
	<li><a href="https://www.joc.or.jp/games/olympic/">日本オリンピック委員会</a></li>
	<li><a href="https://www.2020games.metro.tokyo.jp/taikaijyunbi/olympic/index.html">東京都オリンピック・パラリンピック準備局</a></li>
	<li><a href="https://www.asahi.com/olympics/">朝日新聞デジタル 2020東京オリンピック</a></li>
</ul>
"""
soup = BeautifulSoup(html, "html.parser")
li = soup.find_all(href=re.compile(r"olympic"))
for e in li:print(e.string)

[vagrant@localhost python]$ python3 app.py
日本オリンピック委員会
東京都オリンピック・パラリンピック準備局
朝日新聞デジタル 2020東京オリンピック

print(e.attrs[‘href’])とすると、href属性を取得します。
[vagrant@localhost python]$ python3 app.py
https://www.joc.or.jp/games/olympic/
https://www.2020games.metro.tokyo.jp/taikaijyunbi/olympic/index.html
https://www.asahi.com/olympics/

soup.find(“li”)

findメソッドを使って書くこともできる。

cond = {"data-lo":"android", "class":"free"}
print(soup.find("li", cond).string)

print(soup.find(id="popular")
			.find("li", cond).string)

[vagrant@localhost python]$ python3 app.py
Huawei Technologies P20 liteANE-LX2J
HUAWEI HUAWEI P20 lite