3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

#wikipediaの市町村ページで、市町村位置画像のリンクを取得

Last updated at Posted at 2018-10-02
1 / 2

スクレイピングもといプログラミング超初心者が初投稿してみました。スクレイピングに興味があり、手はじめにWikipediaに挑んでみました。練習なので多めに見てください・・・

nearby_town.py
import urllib
import requests
from bs4 import BeautifulSoup

busho=input("好きな市町村を入力してください:")
url="https://ja.wikipedia.org/wiki/" + urllib.parse.quote_plus(busho, encoding='utf-8')

#URLからhtmlデータを取得
instance = requests.get(url)

# BeautifulSoupで扱えるようにする
soup = BeautifulSoup(instance.text, "html.parser")

#BeautifulSoup形式をListに変換
soup_list=soup.select(".")


j=0
for img in soup_list:
    j+=1
    str_img=str(img)
    if "位置図" in str_img:
        break

location=soup_list[j]
linkss = location.find_all('a')

location_link=[]
location_link.append(linkss[0].attrs['href'])

lurl = 'https://ja.wikipedia.org/' + location_link[-1]
print(lurl)

結果
好きな市町村を入力してください:横浜市
https://ja.wikipedia.org//wiki/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:%E5%9F%BA%E7%A4%8E%E8%87%AA%E6%B2%BB%E4%BD%93%E4%BD%8D%E7%BD%AE%E5%9B%B3_14100.svg

3
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?