0
0

More than 1 year has passed since last update.

Python requests問題とURLの補強

Posted at

行っていたこと

  1. pythonのrequestsを使って複数のURLをチェック(URLが死んでいないか)
  2. URLの補強

問題

問題1 : 脆弱性に該当するSSL通信

エラー内容
<urlopen error [SSL: DH_KEY_TOO_SMALL] dh key too small (_ssl.c:1091)>

requests.get(url, verify = False)

verify = Falseをつけるとセキュリティのレベルを下げてくれるらしい
しかし、エラーとしてではないけど、Warningがめちゃ出る

import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)

問題2 : URLは開けるはずなのに、403で返ってくる

requestsでgetすると403で返ってきてしまう
・・・URLを踏むとUser Agentに応じてステータスコードが異なるように設定されている

USER_AGENT = {"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 Mobile/14G60 Safari/602.1"}

response = requests.get(url, headers=USER_AGENT, verify = False)
print(response.status_code)

headers=USER_AGENTiPhoneで入っていると認識させる必要がある
(pythonのrequestsのデフォルトがpython-requests)

URLの補強

aタグのhrefに/companyなどが検出され、踏んでもページが発見できない

from urllib.parse import urlparse

parsed_url = urlparse(url)
print(parsed_url.scheme) # http or https
print(parsed_url.netloc) # ドメイン

/companyと同一ドメインの時に上記のコードを使うことで、URLを補強して作ることができる!

最後に

大量のURLを叩くほど、見たことないURLエラーが出たので備忘録としても記載しました。
URLのエラ−って沢山あるんだなぁと知りました。
また、新しいエラーがあったら追記します。

参考文献

urllibでも同じようなことができるので、使ってみました

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0