Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2804

診断メーカーから診断結果を取得(Python, Bash)

$
0
0
何? 診断メーカーから結果を取得したい 手段 要Session 診断ページに以下のパラメータをPOST: _token: 診断ページから取得 shindanName: 任意 ここをランダムにするとランダムな結果が得られて楽しい hiddenName: 診断ページから取得 Python #!/usr/bin/env python import random import time from typing import Optional from bs4 import BeautifulSoup as BS import requests def shindan(url: str = 'https://shindanmaker.com/1023289', name: Optional[str] = None) -> str: session = requests.session() s = session.get(url) if s.status_code != 200: raise FileNotFoundError(s.status_code) source = BS(s.text) params = {i['name']: i['value'] for i in source.find_all('input')[1:4]} params['shindanName'] = (str(random.random()) if name is None else name) login = session.post(url, data=params) # 連続で取得する場合はWaitを入れる # time.sleep(random.uniform(2, 5)) return BS(login.text).find('span', id='shindanResult').text shindan() # 'そんなにクレメンしたいんなら永遠にクレメンだけさせてやろうか' Bash #!/usr/bin/env bash shindan() { url="${1-https://shindanmaker.com/1036646}" [ "$( curl -LI "$url" -o /dev/null -w '%{http_code}' -s )" -ne 200 ] && { echo Error>&2 && return 0 } name="${2-$RANDOM}" source="$(curl -s -c cookie.txt "$url")" inputs="$( echo -e "$source" | tr \< \\n | grep input | sed -nr ' s/value=""/value="'$name'"/ s/^.* name="([^"]+)".* value="([^"]+)">$/\1=\2/g 4,6p' | tr \\n \& | sed 's/.$//' )" curl -s -X POST -d "$inputs" -b cookie.txt "$url" | grep -oP '<span id="shindanResult" [^>]+>(.*?)(</span>){5}' | sed -r 's_<br />_'\\n'_g;s/<[^>]+>//g' # 連続で取得する場合はWaitを入れる # sleep "$[RANDOM%4+2]" rm cookie.txt } shindan # ねこって、むしだ。 # # 𝙔𝙊𝙎𝙃𝙄𝙉𝙊𝙔𝘼

Viewing all articles
Browse latest Browse all 2804

Trending Articles