标签:采集 date pip ide 数据信息 parse font test png
一 遍历单个域名
网页爬虫,就是对目标网页进行捉取,然后遍历到数据信息,然后有链接的继续遍历,如此回调。
第一步:将页面的所有链接获取
1 from urllib.request import urlopen 2 from bs4 import BeautifulSoup 3 import re 4 5 html = urlopen("https://www.yahoo.com/") 6 html_str = html.read().decode(‘utf-8‘) 7 #print(html_str) 8 bsObj = BeautifulSoup(html_str) 9 ##获取页面链接地址 10 for link in bsObj.findAll("a"): 11 if ‘href‘ in link.attrs: 12 print(link.attrs[‘href‘])
运行效果图
发现会存在些没用用的数据,有些href的值只是作为页面块的跳转,我们可以使用正则表达式进行优化过滤掉,只获取带有HTML结尾的链接
1 from urllib.request import urlopen 2 from bs4 import BeautifulSoup 3 import re 4 5 html = urlopen("https://www.yahoo.com/") 6 html_str = html.read().decode(‘utf-8‘) 7 #print(html_str) 8 bsObj = BeautifulSoup(html_str) 9 ##获取页面链接地址 10 for link in bsObj.findAll("a" ,href= re.compile(".*\.html")): 11 if ‘href‘ in link.attrs: 12 print(link.attrs[‘href‘])
第二步:递归获取网页
第一步我们基本把一个网页的所有链接地址获取到,第二步显然是获取这些链接网页的链接,进一步获取这些网页数据。
例如我们在Wiki获取Python词条下面的相关词条的链接,由于存在不是我们关心的链接,所有需要正则表达式过滤掉一部分,然后大量的链接的链接的链接,我们不可能穷尽,所有随机获取一些词条。
1 from urllib.request import urlopen 2 from bs4 import BeautifulSoup 3 import re 4 import datetime 5 import random 6 7 rd = random.seed(datetime.datetime.now()) 8 print(rd) 9 10 def getLinks(articleUrl): 11 html = urlopen("https://en.wikipedia.org"+articleUrl) 12 bsObj = BeautifulSoup(html,"lxml") 13 return bsObj.findAll("a",href=re.compile("^(/wiki/)((?!:).)*$")) 14 15 links = getLinks("/wiki/Python") 16 17 while len(links) >0 : 18 #print(links) 19 newArticle = links[random.randint(0, len(links)-1)].attrs["href"]#随机获取一个来继续爬 20 print(newArticle) 21 links = getLinks(newArticle)
运行结果(一分钟150条数据产生,如非手动停止应该不会停止爬取)
二 采集整个网站
对整个站点进行所有链路采集,当然像wiki这些大型网站数据很多,要全部采集基本不可能。
1 from urllib.request import urlopen 2 from bs4 import BeautifulSoup 3 import re 4 pages = set() 5 def getLinks(pageUrl): 6 global pages 7 html = urlopen("http://en.wikipedia.org"+pageUrl) 8 bsObj = BeautifulSoup(html,"lxml") 9 try: 10 print(bsObj.h1.get_text()) 11 print(bsObj.find(id="mw-content-text").findAll("p")[0]) 12 print(bsObj.find(id="ca-edit").find("span").find("a").attrs[‘href‘]) 13 except AttributeError: 14 print("页面缺少一些属性!不过不用担心!") 15 for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")): 16 if ‘href‘ in link.attrs: 17 if link.attrs[‘href‘] not in pages: 18 # 我们遇到了新页面 19 newPage = link.attrs[‘href‘] 20 print("----------------\n"+newPage) 21 pages.add(newPage) 22 getLinks(newPage) 23 getLinks("")
运行结果
递归爬取网页原理:
三 采用Scrapy采集
高楼大厦都是从最简单的一砖一瓦叠起来,写网络爬虫也是很多简单的重复的操作组成,找到页面的关键信息和外链,然后再如此循环。而Scrapy库,可以大幅度降低网页链接查找(不用自己去搞一大堆的过滤条件和正则表达式)还可以降低识别的工作复杂度。
使用参考;https://scrapy-chs.readthedocs.io/zh_CN/latest/intro/tutorial.html
第一步 创建Scrapy项目
报错,安装scrapy,cmd-pip install scrapy
报错,没有安装visual 14
重新安装成功,再次执行
scrapy startproject tutorial
创建成功后,目录结构如下
第二步 定义数据源,修改item(参考官网)
第三步 创建爬虫class(参考官网)
第四步 进入spider目录,然后运行爬虫
报错,缺少win32库
pip install pywin32
再次运行成功
第一个Scrapy的helloworld基本完成,这个过程大致如下:
Scrapy为Spider的 start_urls
属性中的每个URL创建了 scrapy.Request
对象,并将 parse
方法作为回调函数(callback)赋值给了Request。
Request对象经过调度,执行生成 scrapy.http.Response
对象并送回给spider parse()
方法。
如有用到,后面继续深入学习Scrapy。
标签:采集 date pip ide 数据信息 parse font test png
原文地址:https://www.cnblogs.com/visuangel/p/9334919.html