Python爬虫基础

ooed6838 7年前
   <h2>前言</h2>    <p>Python非常适合用来开发网页爬虫,理由如下:</p>    <p>1、抓取网页本身的接口</p>    <p>相比与其他静态编程语言,如java,c#,c++,python抓取网页文档的接口更简洁;相比其他动态脚本语言,如perl,shell,python的urllib2包提供了较为完整的访问网页文档的API。(当然ruby也是很好的选择)</p>    <p>此外,抓取网页有时候需要模拟浏览器的行为,很多网站对于生硬的爬虫抓取都是封杀的。这是我们需要模拟user agent的行为构造合适的请求,譬如模拟用户登陆、模拟session/cookie的存储和设置。在python里都有非常优秀的第三方包帮你搞定,如Requests,mechanize</p>    <p>2、网页抓取后的处理</p>    <p>抓取的网页通常需要处理,比如过滤html标签,提取文本等。python的beautifulsoap提供了简洁的文档处理功能,能用极短的代码完成大部分文档的处理。</p>    <p>其实以上功能很多语言和工具都能做,但是用python能够干得最快,最干净。</p>    <p>Life is short, you need python.</p>    <p>PS:python2.x和python3.x有很大不同,本文只讨论python3.x的爬虫实现方法。</p>    <h2>爬虫架构</h2>    <h2>架构组成</h2>    <p><img src="https://simg.open-open.com/show/b10e36c114a07082f709ab06baf74e25.jpg"></p>    <p>URL管理器:管理待爬取的url集合和已爬取的url集合,传送待爬取的url给网页下载器。</p>    <p>网页下载器(urllib):爬取url对应的网页,存储成字符串,传送给网页解析器。</p>    <p>网页解析器(BeautifulSoup):解析出有价值的数据,存储下来,同时补充url到URL管理器。</p>    <h2>运行流程</h2>    <p><img src="https://simg.open-open.com/show/ac75ce65c499ffcab5990672be418b5e.jpg"></p>    <h2>URL管理器</h2>    <h2>基本功能</h2>    <ul>     <li>添加新的url到待爬取url集合中。</li>     <li>判断待添加的url是否在容器中(包括待爬取url集合和已爬取url集合)。</li>     <li>获取待爬取的url。</li>     <li>判断是否有待爬取的url。</li>     <li>将爬取完成的url从待爬取url集合移动到已爬取url集合。</li>    </ul>    <h2>存储方式</h2>    <p>1、内存(python内存)</p>    <p>待爬取url集合:set()</p>    <p>已爬取url集合:set()</p>    <p>2、关系数据库(mysql)</p>    <p>urls(url, is_crawled)</p>    <p>3、缓存(redis)</p>    <p>待爬取url集合:set</p>    <p>已爬取url集合:set</p>    <p>大型互联网公司,由于缓存数据库的高性能,一般把url存储在缓存数据库中。小型公司,一般把url存储在内存中,如果想要永久存储,则存储到关系数据库中。</p>    <h2>网页下载器(urllib)</h2>    <p>将url对应的网页下载到本地,存储成一个文件或字符串。</p>    <h2>基本方法</h2>    <p>新建baidu.py,内容如下:</p>    <pre>  <code class="language-python">import urllib.request    response = urllib.request.urlopen('http://www.baidu.com')  buff = response.read()  html = buff.decode("utf8")  print(html)</code></pre>    <p>命令行中执行 python baidu.py ,则可以打印出获取到的页面。</p>    <h2>构造Request</h2>    <p>上面的代码,可以修改为:</p>    <pre>  <code class="language-python">import urllib.request    request = urllib.request.Request('http://www.baidu.com')  response = urllib.request.urlopen(request)  buff = response.read()  html = buff.decode("utf8")  print(html)</code></pre>    <h2>携带参数</h2>    <p>新建baidu2.py,内容如下:</p>    <pre>  <code class="language-python">import urllib.request  import urllib.parse    url = 'http://www.baidu.com'  values = {'name': 'voidking','language': 'Python'}  data = urllib.parse.urlencode(values).encode(encoding='utf-8',errors='ignore')  headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0' }  request = urllib.request.Request(url=url, data=data,headers=headers,method='GET')  response = urllib.request.urlopen(request)  buff = response.read()  html = buff.decode("utf8")  print(html)</code></pre>    <h2>使用Fiddler监听数据</h2>    <p>我们想要查看一下,我们的请求是否真的携带了参数,所以需要使用fiddler。</p>    <p>打开fiddler之后,却意外发现,上面的代码会报错504,无论是baidu.py还是baidu2.py。</p>    <p><img src="https://simg.open-open.com/show/727116635ff28c73f1f9bd8b99e75dce.jpg"></p>    <p>虽然python有报错,但是在fiddler中,我们可以看到请求信息,确实携带了参数。</p>    <p><img src="https://simg.open-open.com/show/497f76a690e2682fc4f876a5ad11fa8e.jpg"></p>    <p>经过查找资料,发现python以前版本的Request都不支持代理环境下访问https。但是,最近的版本应该支持了才对。那么,最简单的办法,就是换一个使用http协议的url来爬取,比如,换成 http://www.csdn.net 。结果,依然报错,只不过变成了400错误。</p>    <p><img src="https://simg.open-open.com/show/ef9e484dcf6a42b9a4f5009914f74ac0.jpg"></p>    <p>然而,然而,然而。。。神转折出现了!!!</p>    <p>当我把url换成 http://www.csdn.net/ 后,请求成功!没错,就是在网址后面多加了一个斜杠 / 。同理,把 http://www.baidu.com 改成 http://www.baidu.com/ ,请求也成功了!神奇!!!</p>    <h2>添加处理器</h2>    <p><img src="https://simg.open-open.com/show/31e9f426c356eca37c32bf65558f8e54.jpg"></p>    <pre>  <code class="language-python">import urllib.request  import http.cookiejar    # 创建cookie容器  cj = http.cookiejar.CookieJar()  # 创建opener  opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))  # 给urllib.request安装opener  urllib.request.install_opener(opener)    # 请求  request = urllib.request.Request('http://www.baidu.com/')  response = urllib.request.urlopen(request)  buff = response.read()  html = buff.decode("utf8")  print(html)  print(cj)</code></pre>    <h2>网页解析器(BeautifulSoup)</h2>    <p>从网页中提取出有价值的数据和新的url列表。</p>    <h2>解析器选择</h2>    <p>为了实现解析器,可以选择使用正则表达式、html.parser、BeautifulSoup、lxml等,这里我们选择BeautifulSoup。</p>    <p>其中,正则表达式基于模糊匹配,而另外三种则是基于DOM结构化解析。</p>    <h2>BeautifulSoup</h2>    <h3>安装测试</h3>    <p>1、安装,在命令行下执行 pip install beautifulsoup4 。</p>    <p>2、测试</p>    <pre>  <code class="language-python">import bs4  print(bs4)</code></pre>    <h3>使用说明</h3>    <p><img src="https://simg.open-open.com/show/5d4486c09ea2fb1d41459288287931a0.jpg"> <img src="https://simg.open-open.com/show/9b8a6a5caa34d810f8978f6c5adb89b5.jpg"></p>    <h3>基本用法</h3>    <p>1、创建BeautifulSoup对象</p>    <pre>  <code class="language-python">import bs4  from bs4 import BeautifulSoup    # 根据html网页字符串创建BeautifulSoup对象  html_doc = """  <html><head><title>The Dormouse's story</title></head>  <body>  <p class="title"><b>The Dormouse's story</b></p>    <p class="story">Once upon a time there were three little sisters; and their names were  <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,  <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and  <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;  and they lived at the bottom of a well.</p>    <p class="story">...</p>  """  soup = BeautifulSoup(html_doc)  print(soup.prettify())</code></pre>    <p>2、访问节点</p>    <pre>  <code class="language-python">print(soup.title)  print(soup.title.name)  print(soup.title.string)  print(soup.title.parent.name)    print(soup.p)  print(soup.p['class'])</code></pre>    <p>3、指定tag、class或id</p>    <pre>  <code class="language-python">print(soup.find_all('a'))  print(soup.find('a'))  print(soup.find(class_='title'))  print(soup.find(id="link3"))  print(soup.find('p',class_='title'))</code></pre>    <p>4、从文档中找到所有 <a> 标签的链接</p>    <pre>  <code class="language-python">for link in soup.find_all('a'):      print(link.get('href'))</code></pre>    <p><img src="https://simg.open-open.com/show/e0dafc15f29d48a4d53f3fd805e6310f.jpg"> 出现了警告,根据提示,我们在创建BeautifulSoup对象时,指定解析器即可。</p>    <pre>  <code class="language-python">soup = BeautifulSoup(html_doc,'html.parser')</code></pre>    <p>5、从文档中获取所有文字内容</p>    <pre>  <code class="language-python">print(soup.get_text())</code></pre>    <p>6、正则匹配</p>    <pre>  <code class="language-python">link_node = soup.find('a',href=re.compile(r"til"))  print(link_node)</code></pre>    <h2>后记</h2>    <p>python爬虫基础知识,至此足够,接下来,在实战中学习更高级的知识。</p>    <h2>书签</h2>    <p>Python开发简单爬虫</p>    <p><a href="/misc/goto?guid=4959664774080667097" rel="nofollow,noindex">http://www.imooc.com/learn/563</a></p>    <p>The Python Standard Library</p>    <p><a href="/misc/goto?guid=4959735566404714227" rel="nofollow,noindex">https://docs.python.org/3/library/index.html</a></p>    <p>Beautiful Soup 4.2.0 文档</p>    <p><a href="/misc/goto?guid=4959735566497507352" rel="nofollow,noindex">https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html</a></p>    <p>为什么python适合写爬虫?</p>    <p><a href="/misc/goto?guid=4959735566579793520" rel="nofollow,noindex">http://www.cnblogs.com/benzone/p/5854084.html</a></p>    <p>如何学习Python爬虫[入门篇]?</p>    <p><a href="/misc/goto?guid=4959735566658465630" rel="nofollow,noindex">https://zhuanlan.zhihu.com/p/21479334?refer=passer</a></p>    <p>你需要这些:Python3.x爬虫学习资料整理</p>    <p><a href="/misc/goto?guid=4959735566750288717" rel="nofollow,noindex">https://zhuanlan.zhihu.com/p/24358829?refer=passer</a></p>    <p>如何入门 Python 爬虫?</p>    <p><a href="/misc/goto?guid=4959735566840225838" rel="nofollow,noindex">https://www.zhihu.com/question/20899988</a></p>    <p>Python3.X 抓取网络资源</p>    <p><a href="http://www.open-open.com/lib/view/open1396062681294.html" rel="nofollow,noindex">http://www.open-open.com/lib/view/open1396062681294.html</a></p>    <p>python网络请求和"HTTP Error 504:Fiddler - Receive Failure"</p>    <p><a href="/misc/goto?guid=4959735566929931476" rel="nofollow,noindex">http://blog.csdn.net/guoguo527/article/details/50709244</a></p>    <p>怎么使用Fiddler抓取自己写的爬虫的包?</p>    <p><a href="/misc/goto?guid=4959735567021373588" rel="nofollow,noindex">https://www.zhihu.com/question/52614615</a></p>    <p>fiddler对python脚本抓取https包时发生了错误?</p>    <p><a href="/misc/goto?guid=4959735567110224437" rel="nofollow,noindex">https://www.zhihu.com/question/42104344?sort=created</a></p>    <p>HTTPS和HTTP的区别</p>    <p><a href="/misc/goto?guid=4959735567188703273" rel="nofollow,noindex">http://blog.csdn.net/whatday/article/details/38147103</a></p>    <p> </p>    <p>来自:http://www.cnblogs.com/voidking/p/python-crawler-base.html</p>    <p> </p>