admin管理员组文章数量:1320608
I found the following web scraping code in Web Scraping with Python by Ryan Mitchel:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
pages = set()
def getLinks(pageUrl):
global pages
html = urlopen(";+pageUrl)
bsObj = BeautifulSoup(html)
for link in bsObj.findAll("a", href=repile("^(/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("")
I believe that in the findAll()
for loop, all tag objects with href
attributes that meet the criteria have already been retrieved. Why do we still need to check if the object has the href
attribute afterward?
In my opinion, I think that this line code should be deleted: if 'href' in link.attrs:
Do I think correctly?
本文标签: pythonWhy validate the href attribute twiceStack Overflow
版权声明:本文标题:python - Why validate the `href` attribute twice? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742079708a2419623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论