Код: Выделить всё
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
pages = set()
def getLinks(pageUrl):
global pages
html = urlopen("http://en.wikipedia.org"+pageUrl)
bsObj = BeautifulSoup(html)
for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")):
if 'href' in link.attrs:
if link.attrs['href'] not in pages:
#find new page
newPage = link.attrs['href']
print(newPage)
pages.add(newPage)
getLinks(newPage)
getLinks("")
На мой взгляд, я считаю, что этот код строки следует удалить: если 'href' в link.attrs: Правильно ли я думаю?
Подробнее здесь: https://stackoverflow.com/questions/793 ... bute-twice
Мобильная версия