Last active
July 17, 2019 09:20
-
-
Save tadaken3/9b96fd718f57ed278d0a5f169f1c05c5 to your computer and use it in GitHub Desktop.
wikipediaからのページからリンク一覧を取得する方法 ref: https://qiita.com/tadaken3/items/e09ba2ede988bbacb303
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#codeing:utf-8 | |
import re | |
from bs4 import BeautifulSoup | |
from urllib.request import urlopen | |
from urllib.parse import unquote | |
url = "https://ja.wikipedia.org/wiki/%E3%83%86%E3%82%A4%E3%83%AB%E3%82%BA_%E3%82%AA%E3%83%96_%E3%82%A4%E3%83%8E%E3%82%BB%E3%83%B3%E3%82%B9" | |
html = urlopen(url) | |
bsObj = BeautifulSoup(html,'html.parser') | |
pattern = re.compile("^(/wiki/)((?!:).)*$") | |
for link in bsObj.find('div',{'id':'bodyContent'}).findAll('a',href = pattern): | |
if 'href' in link.attrs: | |
print (unquote(link.attrs['href'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#codeing:utf-8 | |
import re | |
from bs4 import BeautifulSoup | |
from urllib.request import urlopen | |
from urllib.parse import unquote | |
url = "https://ja.wikipedia.org/wiki/%E3%83%86%E3%82%A4%E3%83%AB%E3%82%BA_%E3%82%AA%E3%83%96_%E3%82%A4%E3%83%8E%E3%82%BB%E3%83%B3%E3%82%B9" | |
html = urlopen(url) | |
bsObj = BeautifulSoup(html,'html.parser') | |
pattern = re.compile("^(/wiki/)((?!:).)*$") | |
for link in bsObj.find('div',{'id':'bodyContent'}).findAll('a',href = pattern): | |
if 'href' in link.attrs: | |
print (unquote(link.attrs['href'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment