スクレイピング対策

ヘッドレスChromeJavaScriptを有効にする teratail.com

スクレイピング https://gather-tech.info/news/2017/08/14/Gather59.html

ヘッドレスChromeからのアクセスを検出する方法について。User Agentによる判定、プラグインの有無による判定、画像読み込みがエラーになるときのサイズによる判定方法などが紹介されている。

一般的なUser Agentによる判定だと簡単に偽装できてしまうのでそれ以外の検出方法が書かれていて参考になります。画像が読み込めない場合のサイズの違いで検出できるとは驚きです。

Detecting Chrome Headless https://antoinevastel.com/bot%20detection/2017/08/05/detect-chrome-headless.html

qiita.com

こちらの再現性を低くするぐらいだったらまだ大丈夫かな

「日経」の文字のタグを検索してその子を抽出でいける。 数字以外は消せばいいので「\D」で置換

from bs4 import BeautifulSoup
import re


def nikkei_find(tag):
    return ('日経' in tag.contents[0])


doc = """<div>日経平均株価<b>114514</b></div>
<div>日経平均株価<span>114514</span></div>
<div>日経平均株価<div id="yaju">114514</div></div>"""

soup = BeautifulSoup(doc, 'html.parser')

for i in soup.find_all(nikkei_find):
    text = re.sub(r'\D', '', i.contents[1].get_text(strip=True))
    print(text)

doc = """<div>日経平均株価<b>114514</b></div>
<div>日経平均株価<b>114,514</b></div>
<div>日経平均株価<b>114 514</b></div>"""

soup = BeautifulSoup(doc, 'html.parser')

for i in soup.find_all(nikkei_find):
    text = re.sub(r'\D', '', i.contents[1].get_text(strip=True))
    print(text)