<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>爬虫 on 庞玉栋个人博客</title><link>https://pangyd.com/tags/%E7%88%AC%E8%99%AB/</link><description>Recent content in 爬虫 on 庞玉栋个人博客</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Sat, 18 Aug 2018 09:20:35 +0800</lastBuildDate><atom:link href="https://pangyd.com/tags/%E7%88%AC%E8%99%AB/index.xml" rel="self" type="application/rss+xml"/><item><title>爬虫分享【1】</title><link>https://pangyd.com/post/196-1/</link><pubDate>Sat, 04 Aug 2018 20:34:47 +0800</pubDate><guid>https://pangyd.com/post/196-1/</guid><description>&lt;p>&lt;/p>&lt;p>
&lt;/p>&lt;p>&lt;span>&lt;span lang="EN-US">Python&lt;/span>&lt;/span>&lt;span>&lt;span>环境：&lt;span lang="EN-US">3.6&lt;/span>&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">&lt;/span>&lt;/span>&lt;/p>
&lt;p>&lt;span>&lt;span>需要的库：&lt;span lang="EN-US">requests &lt;/span>基于&lt;span lang="EN-US">urllib &lt;/span>比&lt;span lang="EN-US">urllib&lt;/span>更加的简洁，&lt;/span>&lt;/span>&lt;span lang="EN-US">BeautifulSoup &lt;/span>&lt;span>：网页解析库&lt;/span>&lt;span>&lt;span lang="EN-US">&lt;/span>&lt;/span>&lt;/p>
&lt;p>&lt;span>&lt;span lang="EN-US">&lt;span>1，&lt;span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span>&lt;/span>&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">headers &lt;/span>&lt;/span>&lt;span>&lt;span>头部分 用于向服务器发送信息 伪装成浏览器访问&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">&lt;/span>&lt;/span>&lt;/p>
&lt;p>&lt;span>&lt;span lang="EN-US">&lt;span>2，&lt;span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span>&lt;/span>&lt;/span>&lt;/span>&lt;b>&lt;span lang="EN-US">def
&lt;/span>&lt;/b>&lt;span lang="EN-US">save_artist(group_id, initial):&lt;br>
&lt;/span>&lt;span>&lt;span lang="EN-US">params&lt;/span>&lt;/span>&lt;span>&lt;span>：字典，用于组合传递过来的参数&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">&lt;/span>&lt;/span>&lt;/p>
&lt;p>&lt;span lang="EN-US">&lt;span>3，&lt;span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span>&lt;/span>&lt;/span>&lt;span lang="EN-US">r&lt;/span>&lt;span>：接收爬虫向服务器发送的参数返回的数据&lt;/span>&lt;span lang="EN-US">&lt;/span>&lt;/p>
&lt;p>&lt;span>&lt;span lang="EN-US">&lt;span>4，&lt;span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span>&lt;/span>&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">soup&lt;/span>&lt;/span>&lt;span>&lt;span>：参数有&lt;span lang="EN-US">html&lt;/span>的文档以及设置字符串的编码格式，&lt;span lang="EN-US">htnl&lt;/span>解析器&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">&lt;/span>&lt;/span>&lt;/p>
&lt;p>&lt;span>&lt;span lang="EN-US">&lt;span>5，&lt;span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span>&lt;/span>&lt;/span>&lt;/span>&lt;span>&lt;span>获取&lt;span lang="EN-US">soup&lt;/span>的&lt;span lang="EN-US">body&lt;/span>标签，也就是返回的&lt;span lang="EN-US">html&lt;/span>文档的&lt;span lang="EN-US">body &lt;/span>，&lt;span lang="EN-US">attrs&lt;/span>字典参数，如果标签字符中带有&lt;span lang="EN-US">-&lt;/span>以及&lt;span lang="EN-US"> class&lt;/span>不能看作标签属性的时候 用字典参数进行传旨，&lt;span lang="EN-US">artists&lt;/span>和上部分类似&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">&lt;/span>&lt;/span>&lt;/p>
&lt;p>&lt;span>&lt;span lang="EN-US">&lt;span>6，&lt;span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span>&lt;/span>&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">For&lt;/span>&lt;/span>&lt;span>&lt;span>循环 将查询到的列表通过遍历进行更深层的数据提取并且转换成格式相对的数据&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">&lt;/span>&lt;/span>&lt;/p>
&lt;p>&lt;span>&lt;span lang="EN-US">&lt;span>7，&lt;span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span>&lt;/span>&lt;/span>&lt;/span>&lt;span>&lt;span>然后调用&lt;span lang="EN-US">sql&lt;/span>命令插入到数据库&lt;/span>&lt;/span>&lt;span>&lt;span lang="EN-US">&lt;/span>&lt;/span>&lt;/p>
&lt;p>&lt;span>&lt;span lang="EN-US">&lt;span>8，&lt;span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span>&lt;/span>&lt;/span>&lt;/span>&lt;span>&lt;span>函数外面定义爬取网页的参数 &lt;span>&amp;nbsp;&lt;/span>并且定义&lt;span lang="EN-US">for&lt;/span>循环以及&lt;span lang="EN-US">range&lt;/span>函数进行有规律的调用&lt;span lang="EN-US"> sava_artist&lt;/span>进行爬取&lt;/span>&lt;/span>&lt;/p>&lt;p>&lt;/p>&lt;pre lay-lang="Python" class="layui-code" skin="notepad">"""
获取所有的歌手信息
"""
import requests
from bs4 import BeautifulSoup
from sql import *
&lt;p>headers = {
&amp;lsquo;Accept&amp;rsquo;: &amp;rsquo;text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,&lt;em>/&lt;/em>;q=0.8&amp;rsquo;,
&amp;lsquo;Accept-Encoding&amp;rsquo;: &amp;lsquo;gzip, deflate, sdch&amp;rsquo;,
&amp;lsquo;Accept-Language&amp;rsquo;: &amp;lsquo;zh-CN,zh;q=0.8,en;q=0.6&amp;rsquo;,
&amp;lsquo;Cache-Control&amp;rsquo;: &amp;rsquo;no-cache&amp;rsquo;,
&amp;lsquo;Connection&amp;rsquo;: &amp;lsquo;keep-alive&amp;rsquo;,
&amp;lsquo;Cookie&amp;rsquo;: &amp;lsquo;_ntes_nnid=7eced19b27ffae35dad3f8f2bf5885cd,1476521011210; _ntes_nuid=7eced19b27ffae35dad3f8f2bf5885cd; usertrack=c+5+hlgB7TgnsAmACnXtAg==; Province=025; City=025; NTES_PASSPORT=6n9ihXhbWKPi8yAqG.i2kETSCRa.ug06Txh8EMrrRsliVQXFV_orx5HffqhQjuGHkNQrLOIRLLotGohL9s10wcYSPiQfI2wiPacKlJ3nYAXgM; &lt;a href="mailto:P_INFO=hourui93@163.com">P_INFO=hourui93@163.com&lt;/a>|1476523293|1|study|11&amp;amp;12|jis&amp;amp;1476511733&amp;amp;mail163#jis&amp;amp;320100#10#0#0|151889&amp;amp;0|g37_client_check&amp;amp;mailsettings&amp;amp;mail163&amp;amp;study&amp;amp;blog|hourui93@163.com; NTES_SESS=Fa2uk.YZsGoj59AgD6tRjTXGaJ8_1_4YvGfXUkS7C1NwtMe.tG1Vzr255TXM6yj2mKqTZzqFtoEKQrgewi9ZK60ylIqq5puaG6QIaNQ7EK5MTcRgHLOhqttDHfaI_vsBzB4bibfamzx1.fhlpqZh_FcnXUYQFw5F5KIBUmGJg7xdasvGf_EgfICWV; S_INFO=1476597594|1|0&amp;amp;80##|hourui93; NETEASE_AUTH_SOURCE=space; NETEASE_AUTH_USERNAME=hourui93; _ga=GA1.2.1405085820.1476521280; JSESSIONID-WYYY=cbd082d2ce2cffbcd5c085d8bf565a95aee3173ddbbb00bfa270950f93f1d8bb4cb55a56a4049fa8c828373f630c78f4a43d6c3d252c4c44f44b098a9434a7d8fc110670a6e1e9af992c78092936b1e19351435ecff76a181993780035547fa5241a5afb96e8c665182d0d5b911663281967d675ff2658015887a94b3ee1575fa1956a5a%3A1476607977016; &lt;em>iuqxldmzr&lt;/em>=25; __utma=94650624.1038096298.1476521011.1476595468.1476606177.8; __utmb=94650624.20.10.1476606177; __utmc=94650624; __utmz=94650624.1476521011.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)&amp;rsquo;,
&amp;lsquo;DNT&amp;rsquo;: &amp;lsquo;1&amp;rsquo;,
&amp;lsquo;Host&amp;rsquo;: &amp;lsquo;music.163.com&amp;rsquo;,
&amp;lsquo;Pragma&amp;rsquo;: &amp;rsquo;no-cache&amp;rsquo;,
&amp;lsquo;Referer&amp;rsquo;: &amp;lsquo;&lt;a href="https://music.163.com/'">https://music.163.com/'&lt;/a>,
&amp;lsquo;Upgrade-Insecure-Requests&amp;rsquo;: &amp;lsquo;1&amp;rsquo;,
&amp;lsquo;User-Agent&amp;rsquo;: &amp;lsquo;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36&amp;rsquo;
}&lt;/p></description></item><item><title>python爬虫——写出最简单的网页爬虫</title><link>https://pangyd.com/post/97-python/</link><pubDate>Sat, 09 Sep 2017 17:08:42 +0800</pubDate><guid>https://pangyd.com/post/97-python/</guid><description>&lt;section data-tools="135编辑器" data-id="89974" style="text-align: justify">&lt;section data-id="us538844">&lt;section>&lt;p>&lt;span class="bannerfix-wrapper" style="text-align: center">&lt;img data-s="300,640" data-type="jpeg" data-src="//qqpublic.qpic.cn/qq_public/0/0-2599578271-A1CD3C3D44AED332B9085FFD7BA070AD/0?fmt=jpg&amp;size=31&amp;rs=23-21&amp;h=427&amp;w=640&amp;ppv=1" data-ratio="0.66640625" data-w="1280" src="https://pangyd.com/uploads/2017/09/1b8214ac44494614.jpg" data-fail="0" data-ueid="20895" width="640" height="auto" data-width="640" data-height="427" data-org="https://mmbiz.qpic.cn/mmbiz_jpg/Mzws9oBx0P7kQpiaL4oQG7TZxbZ6hwFdlbmh5WVswicJbvugib0w0ujibUqXjvUgFuUSme9lNFeDeiaVoric6jSpvmGA/640?wx_fmt=jpeg&amp;wxfrom=5&amp;wx_lazy=1" data-lazy="0">&lt;/span>&lt;/p>&lt;/section>&lt;/section>&lt;/section>&lt;section data-id="us538844" style="text-align: justify">&lt;section>&lt;section>&lt;p>&lt;span>在我们日常上网浏览网页的时候，经常会看到一些好看的图片，我们就希望把这些图片保存下载，或者用户用来做桌面壁纸，或者用来做设计的素材。&lt;/span>&lt;span>我们可以通过&lt;/span>&lt;span>python&amp;nbsp;&lt;/span>&lt;span>来实现这样一个简单的爬虫功能，把我们想要的代码爬取到本地。下面就看看如何使用&lt;/span>&lt;span>python&lt;/span>&lt;span>来实现这样一个功能。&lt;/span>&lt;/p>&lt;/section>&lt;section data-width="100%">&lt;section>&lt;img data-src="//qqpublic.qpic.cn/qq_public/0/0-2516127415-94DD69FAB0632899CD9E62C400ADDFE6/0?fmt=png&amp;size=1&amp;rs=1-0&amp;h=37&amp;w=43&amp;ppv=1" data-ratio="0.8604651162790697" data-w="43" data-type="png" src="https://pangyd.com/uploads/2017/09/1b8214ac44494614.jpg" data-fail="0" data-ueid="592180" width="43" height="auto" data-width="43" data-height="37" data-org="https://mmbiz.qpic.cn/mmbiz_png/Mzws9oBx0P7kQpiaL4oQG7TZxbZ6hwFdlSWdbiaWc3zB05Dc3TN73GB0wFsJZ2xN6V1BlNytSzCqpkOSErJWEKRQ/0.png?wxfrom=5&amp;wx_lazy=1" data-lazy="0">&lt;/section>&lt;/section>&lt;/section>&lt;/section>&lt;section data-id="us538906" style="text-align: justify">&lt;section>&lt;section data-width="100%">&lt;section data-brushtype="text">&lt;span>&lt;strong>开发工具&lt;/strong>&lt;/span>&lt;/section>&lt;/section>&lt;/section>&lt;/section>&lt;p style="text-align: justify">&lt;span>笔者使用的工具是sublimetext3，它的短小精悍（可能男人们都不喜欢这个词）使我十分着迷。推荐大家使用，当然如果你的电脑配置不错，pycharm可能更加适合你。&lt;/span>&lt;/p>&lt;p style="text-align: justify">&lt;span>sublime text3搭建python开发环境推荐查看此博客：&lt;/span>&lt;/p>&lt;p style="text-align: justify">&lt;span>[sublime搭建python开发环境]&lt;/span>&lt;span>[https://www.cnblogs.com/codefish/p/4806849.&lt;/span>&lt;/p>&lt;section data-id="us538906" style="text-align: justify">&lt;section>&lt;section data-width="100%">&lt;section data-brushtype="text">&lt;span>&lt;strong>爬虫介绍&lt;/strong>&lt;/span>&lt;/section>&lt;/section>&lt;/section>&lt;/section>&lt;p style="text-align: justify">&lt;span>&lt;span>爬虫顾名思义，就是像虫子一样，爬在&lt;/span>Internet&lt;span>这张大网上。如此，我们便可以获取自己想要的东西。&lt;/span>&lt;/span>&lt;/p>&lt;p style="text-align: justify">&lt;span>&lt;span>既然要爬在&lt;/span>Internet&lt;span>上，那么我们就需要了解&lt;/span>URL&lt;span>，法号&lt;/span>“&lt;span>统一资源定位器&lt;/span>”&lt;span>，小名&lt;/span>“&lt;span>链接&lt;/span>”&lt;span>。其结构主要由三部分组成：&lt;/span>&lt;/span>&lt;/p>&lt;p style="text-align: justify">&lt;span>&lt;span>（&lt;/span>1&lt;span>）协议：如我们在网址中常见的&lt;/span>HTTP&lt;span>协议。&lt;/span>&lt;/span>&lt;/p>&lt;p style="text-align: justify">&lt;span>&lt;span>（&lt;/span>2&lt;span>）域名或者&lt;/span>IP&lt;span>地址：域名，如：&lt;/span>www.baidu.com&lt;span>，&lt;/span>IP&lt;span>地址，即将域名解析后对应的&lt;/span>IP&lt;span>。&lt;/span>&lt;/span>&lt;/p>&lt;p style="text-align: justify">&lt;span>&lt;span>（&lt;/span>3&lt;span>）路径：即目录或者文件等。&lt;/span>&lt;/span>&lt;/p>&lt;section data-id="us538906" style="text-align: justify">&lt;section>&lt;section data-width="100%">&lt;section data-brushtype="text">&lt;strong>urllib开发最简单的爬虫&lt;/strong>&lt;/section>&lt;/section>&lt;/section>&lt;/section>&lt;h2 style="text-align: justify">&lt;strong>（1）urllib简介&lt;/strong>&lt;/h2>&lt;p style="text-align: justify">&lt;span class="bannerfix-wrapper" style="text-align: center">&lt;img data-s="300,640" data-type="png" data-src="//qqpublic.qpic.cn/qq_public/0/0-2934665086-C4DD3D1B4A848A0F5D9739358CE38612/0?fmt=png&amp;size=12&amp;rs=9-8&amp;h=202&amp;w=837&amp;ppv=1" data-ratio="0.24133811230585425" data-w="837" src="https://pangyd.com/uploads/2017/09/1b8214ac44494614.jpg" data-fail="0" data-ueid="266081" width="837" height="auto" data-width="837" data-height="202" data-org="https://mmbiz.qpic.cn/mmbiz_png/Mzws9oBx0P7kQpiaL4oQG7TZxbZ6hwFdlbAnCoicvLvF2aLKfpCqp6NaztGRzkTQvN6LRN0av2u6XVBjA2N1b7LA/0.png?wxfrom=5&amp;wx_lazy=1" data-lazy="0">&lt;/span>&lt;/p></description></item><item><title>庞玉栋:浅谈seo优化对于网站建设的重要性</title><link>https://pangyd.com/post/91-seo/</link><pubDate>Mon, 04 Sep 2017 19:09:42 +0800</pubDate><guid>https://pangyd.com/post/91-seo/</guid><description>&lt;p>根据最近做SEO优化经验而写 &amp;nbsp;写的也都是我的方法 大神勿喷&lt;/p>&lt;p>SEO：英文Search Engine Optimization缩写而来， 中文意译为搜索引擎优化&amp;nbsp;&lt;/p>&lt;p>&lt;br>&lt;/p>&lt;p>如果你连个网站都没有那就点这里：&lt;a target="_self" href="https://pangyd.com/post/90/">&lt;b>如何拥有一个属于自己的网站&lt;/b>&lt;/a>&lt;/p>&lt;p>&lt;br>&lt;/p>&lt;p>现在的中国搜索引擎 百度是无可置疑的老大 &amp;nbsp;在以前 是谷歌 百度 和soso平分天下 只从谷歌退出江湖 &amp;nbsp;百度就成了第一 soso自然也就排名第二 那为什么现在看不到soso的存在了呢。就是因为soso被搜狗并购 然后360搜索接踵而来 &amp;nbsp;现在最主要的搜索引擎也就成了 百度 搜狗 360搜索&lt;/p>&lt;p>&lt;br>&lt;/p>&lt;p>一个网站的建设除了把网站搭建好 你还要让搜索引擎喜欢你 &amp;nbsp;要不然你的网站就是一个死站 &amp;nbsp;基本上没有什么人访问&lt;/p>&lt;p>&lt;br>&lt;/p>&lt;p>那怎么样才能让搜索引擎喜欢你呢 &amp;nbsp;&lt;/p>&lt;p>简洁的站点结构 代码结构良好 层次分明 这样更容易吸引搜索引擎的爬虫光临 收录你的网站&lt;/p>&lt;p>第一就是你网页的内容 &amp;nbsp;爬虫会先爬取你meta标签里的内容 &amp;nbsp;所以网页的内容一定要和关键字想匹配还有就是关键词的密度一定要在全文占比的2%左右 &amp;nbsp;可以用站长工具的密度查询工具&lt;/p>&lt;p>第二是锚链接 &amp;nbsp;锚链接就是你网页内容中文本所链接超链接 &amp;nbsp;爬虫更喜欢爬这种页面 &amp;nbsp;但是锚链接一定要跟文本内容相匹配 &amp;nbsp;再之就是不要过过 &amp;nbsp;一个页面的锚链接在两个左右就可以&lt;/p>&lt;p>第三是图片的alt标签一定要写上 &amp;nbsp;可以在上面加点关键词 以增加网页关键词的密度 &amp;nbsp;一举两得 &amp;nbsp;&lt;/p>&lt;p>有一句话就是 &amp;nbsp;内容为皇 &amp;nbsp;外链为王 &amp;nbsp;&lt;/p>&lt;p>网页的内容为主要的 其次就是你域名在别的网站所在的数量 &amp;nbsp;建议多在各种博客和论坛多写点文章 &amp;nbsp;然后把超链接加到文本上 &amp;nbsp;这样会增加外链的数量 &amp;nbsp;还有就是选择权重和收录量比较高的网站 &amp;nbsp;这样怕虫子在爬网站的时候会查看你的超链接 &amp;nbsp;吸引爬虫更多的光临你的网站 &amp;nbsp;然后还有友链 就是友情链接 可以跟别的网站交换友情链接 &amp;nbsp;这也是seo优化的一部分 &amp;nbsp;&lt;/p></description></item></channel></rss>