Python 网络数据采集
第一篇博客,用来记录自己的学习。今天因为request过多,被wiki给封掉ip。
贴一个小爬虫:
pages = set() # 声明一个集合,用来去重
def get_link(pageUrl):
"""搜寻一个页面内容里含有/wiki/的href属性链接,进入搜寻到的第一个链接内继续循环,并将其加入集合进行去重"""
global pages
html = requests.get('http://en/wikipedia.org'+pageUrl).text
soup = Beautiful(html,'html.parser')
for link in soup.all("a",href=re.compile('^(/wiki/)'):
if link.attrs['href'] is not None:
if link.attrs['href'] not in pages:
newLink = link.attrs['href']
print(newlink)
pages.add('http://en.wikipedia.org' + newlink)
with open('pages.txt','w') as f:
#用join把集合串接成字符串
pageStr = '\n'.join(pages)
f.writer(pageStr)
get_link(newlink)
# 第一次启动函数时传入空格参数就行了
get_link('')
这个函数看了几遍后现在还算是理解了。但是运行时会有很多意外发生,比如网络出错,包括httperror,urlerror。以及一些很复杂的结构上的错误,有时候url会无限循环导致爬虫崩溃。