Published Date : 2020年9月24日7:53

【python】住所を使ったデータ処理:scrapy shell編
【python】Data processing using address: Scrapy Shell


This blog has an English translation


YouTubeにアップした動画、「【python】住所を使ったデータ処理:scrapy shell編」の補足説明の記事です。

Here's a little more about the 「【python】Data processing using address: Scrapy Shell」 video I uploaded to YouTube.


目次

Table of Contents




① 動画の説明
① Video Description



最初は[街区レベル位置参照情報 国土交通省]のデータを使ってみようかと思いましたが、

At first, I thought about using the data of [City Level Location Reference Information Ministry of Land, Infrastructure, Transport and Tourism],

そのデータがあまり使えそうに無かったので、別のサイトを使って住所をスクレイピングして集めていきます。

but I didn't think I could use that data much, so I use another site to scrape the address and collect it.

※例えば千代田区六番町2と六番町4の緯度経度が同じ等。

※For example, latitude and longitude of 2 Rokuban-cho and 4 Rokuban-cho, Chiyoda Ward are the same.

日本全国の住所が細かく掲載されているサイトです。今回はこのサイトを使わせて頂きます。

It is a website that shows detailed addresses all over Japan. I will use this site this time.

始めに、挙動を確かめる為、scrapy shellを立ち上げます。

First, to see how it works, you need to start up a scrapy shell.

scrapy shell "URL"

あらかじめブラウザのインスペクタを利用して、サイトの構造を確かめておいて、何度も使うXPATHを変数に格納します。

Use the browser inspector to determine the structure of the site in advance, and store the XPATH in a variable that you use many times.

input_text_field_xpath = '//input[@class="input_text_field"]'

最初に調べる都道府県のURLとベースとなるURLを繋げて、フェッチメソッドでそのURLにアクセスします。

You join the URL of the prefecture to be checked first with the base URL, and access the URL with the fetch method.

fetch(base_url + prefecture_url)

レスポンスのステータスコードが200なら正常にアクセスできています。これを判定用の変数に格納しましょう。

If the status code of the response is 200, the access is successful. This is stored in a variable for use in a conditional branch.

response.status
200
status_code = response.status
if statuscode == 200

住所が表示されているHTMLタグと、次の街区や番地へのリンクが格納されているHTMLタグを取得します。

Gets the HTML tag that contains the address and links to the next block or street address.

address_box = response.xpath(address_box_xpath)
next_page_link = response.xpath(next_page_link_xpath)

そして取得したエレメントからテキスト情報を抜き出します。

It then extracts the text information from the retrieved elements.

address_text = response.xpath(address_text_xpath).get()
address_list = address_box.getall()

また、リンクが格納されたリストは重複を避ける為、一度setメソッドを使った後再度リストに変換します。

Also, the list containing the link is converted to a list again after using the set method to avoid duplication.

link_list = list(set([next_page.get() for next_page in next_page_link]))

先ほどのリストと、ステータスコードを使って、させたい処理を分岐させます。

Use the previous list and the status code to branch out the operations you want to perform.

if statuscode == 200 and link_list != []:

timeモジュールをインポートして、リクエストの間隔を3秒ほど設けます。サーバーに負担をかけないようにするためです。

Import the time module and allow 3 seconds between requests. It is to avoid putting a burden on the server.

import time
fetch(next_page)
time.sleep(3)

それでは先ほどの処理を全て繋げて、一回だけFor文を使って挙動を確かめてみましょう。

Now, let's combine all the previous steps and use the For statement once to see how it works.

無事、サイトの一番深いURLの階層の住所が取得できました。

We have successfully obtained the address of the deepest URL hierarchy on the site.

後はこれらの処理を全てのリンクに適用させて、再帰的に住所の情報を取得できるようにする必要があります。

You then need to apply these processes to all links so that address information can be retrieved recursively.



以上です。お疲れ様です。

That's all. Thank you for your hard work.