Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
finished 3-2-1
  • Loading branch information
masamichiIto committed Oct 5, 2023
commit 9a835fde92507e0fde2968a07f0f5701840358b8
24 changes: 23 additions & 1 deletion chap3_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,26 @@ def getLinks(pageUrl):
getLinks('')
"""

## 3-2-1
## 3-2-1
pages = set()
def getLinks(pageUrl):
global pages
html = urlopen('http://en.wikipedia.org{}'.format(pageUrl))
bs = BeautifulSoup(html, 'html.parser')
try:
print(bs.h1.get_text())
print(bs.find(id='mw-content-text').find_all('p')[0])
print(bs.find(id='ca-edit').find('span').find('a').attrs['href'])
except AttributeError:
print('This page is missing something! Continuing.')

for link in bs.find_all('a', href=re.compile('^(/wiki/)')):
if 'href' in link.attrs:
if link.attrs['href'] not in pages:
# 新しいページに出会った
newPage = link.attrs['href']
print('-'*20)
print(newPage)
pages.add(newPage)
getLinks(newPage)
getLinks('')