プログラムの中の関数 getLinks についてなのですが、基本的な事が分かりません。
①関数の定義は print("This page is missing somethoing.")までですか。それともgetLinks(newPage)までですか。
python
1from urllib.request import urlopen 2from bs4 import BeautifulSoup 3import re 4 5pages = set() 6 7def getLinks(pageUrl): 8 global pages 9 html = urlopen("http://en.wikipedia.org"+pageUrl) 10 bsObj = BeautifulSoup(html) 11 try: 12 print(bsObj.h1.get_text()) 13 print(bsObj.find(id ="mw-content-text").findAll("p")[0]) 14 print(bsObj.find(id ="ca-edit").find("span").find("a").attrs['href']) 15 except AttributeError: 16 print("This page is missing somethoing.") 17 18 for link in bsObj.findAll("a", href=re.compile("ˆ(/wiki/)")): 19 if 'href' in link.attrs: 20 if link.attrs['href'] not in pages: 21 newPage = link.attrs['href'] 22 print("----------------\n"+newPage) 23 pages.add(newPage) 24 getLinks(newPage) 25getLinks("")
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/24 04:22
2017/09/24 04:43
2017/09/24 10:31
2017/09/24 13:01