1
1

More than 5 years have passed since last update.

細かすぎて伝わらない一秒でクローラーになるワンライナー

Last updated at Posted at 2017-11-30

細かすぎて伝わらない一秒でクローラーになるワンライナ

いきなりずるいワンライナーでごめんなさいmm

注意事項

Webスクレイピングの注意事項一覧
を一読してから実行してください。

自己責任でお願いします。

やっていること

  • sitemapから<loc></loc> を抜き出してその内容をファイルに書き出す
  • sleepを3秒ずついれている
  • 取得したコンテンツの内容をshell実行直下のファイルに書き出している
  • ネストしたsitemap用に書いています。単体のsitemapの場合は個人で工夫してください

細かすぎて伝わらない一秒でクローラーになるワンライナ

URL="{取得したいサイトのURL}";[ -e site_map_urls.txt ] && rm -rf site_map_urls.txt; for xml_sitemap_url in $(curl -s "$URL" | grep "<loc>.*</loc>"|sed -e s/\<\\/loc\>//g | sed -e "s/<loc>//g" | tr -d '\t'); do for url in $(curl -s "$xml_sitemap_url" | grep "<loc>.*</loc>"| sed -e s/\<\\/loc\>//g | sed -e "s/<loc>//g"|tr -d '\t'); do echo $url >> site_map_urls.txt; done; done; for url in `cat site_map_urls.txt | sort | uniq`; do file_name=$(echo $url | tr ":" "-" | tr "/" "-"); curl -sS $url > "$file_name.txt"; sleep 3; done;

原文

#!/usr/bin/env bash
URL="{取得したいサイトのsitemapurl}";[ -e site_map_urls.txt ] && rm -rf site_map_urls.txt;
rm -f site_map_urls.txt;
for xml_sitemap_url in $(curl -s "$URL" | grep "<loc>.*</loc>"|sed -e s/\<\\/loc\>//g | sed -e "s/<loc>//g" | tr -d '\t')
do
  for url in $(curl -s "$xml_sitemap_url" | grep "<loc>.*</loc>"| sed -e s/\<\\/loc\>//g | sed -e "s/<loc>//g"|tr -d '\t')
  do
    echo $url >> site_map_urls.txt
  done
done
for url in `cat site_map_urls.txt | sort | uniq`; do file_name=$(echo $url | tr ":" "-" | tr "/" "-"); curl -sS $url > "$file_name.txt"; sleep 3; done;
1
1
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
1
1