美文网首页大数据 爬虫Python AI SqlPython小哥哥
美团商家信息采集神器 !Python采集所有商家信息!

美团商家信息采集神器 !Python采集所有商家信息!

作者: 14e61d025165 | 来源:发表于2019-06-09 15:19 被阅读3次

在github上找到一个美团的项目,可以获取指定城市的商户信息,分分钟几百个商户信息的数据就到手里,信息中包含商店名、地理位置、评分、销量、telephone(这个是重点)。

好久没有更新了,今天写文章的同时附上这个很有价值的项目下载地址。

本文是自己写的xpath笔记,大家不想看可以直接翻到文章最下面代码下载地址。

项目很可惜是用是scrapy写的,其实我不愿意用框架,但是为了将这个项目改成能运行的代码,耗费了一天时间。在改动过程中又重新熟悉了scrapy。决定还是学学xpath,以后动手用scrapy写几个爬虫。

Python学习交流群:1004391443,这里是python学习者聚集地,有大牛答疑,有资源共享!小编也准备了一份python学习资料,有想学习python编程的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。

scrapy的选择器除了css,就属xpath比较好用。现在需要练习下xpath的使用。

Xpath简介

一般来说,使用id、name、class等属性就能对节点进行定位就能解决绝大部分解析需求,但有时候遇到以下情况,使用Xpath就更方便:

  1. 没有id、name、class等
  2. 标签的属性或者文本特征不显著
  3. 标签嵌套层次太复杂

Xpath是XML Path的简介,基于XML树状结构,可以在整个树中寻找锁定目标节点。由于HTML文档本身就是一个标准的XML页面,因此我们可以使用XPath的语法来定位页面元素。

Xpath定位方法

一、Xpath路径

符号名称含义/绝对路径表示从根节点开始选取//相对路径表示选择从任意位置的某个节点,而不考虑他们的位置

Xpath路径案例

定位节点

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#查找html下的body下的form下的所有input节点
/html/body/form/input

查找所有input节点

//input
</pre>

通配符选择未知的节点*

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#查找form节点下的所有节点
//form/#查找所有节点//

查找所有input节点(input至少有爷爷辈亲戚节点)

//*/input
</pre>

二、使用索引(这是我自己理解)

如果筛选时元素时出现多个节点,但我们想确定唯一节点。可以使用类似于列表索引的方式精确定位。

案例

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#定位 第8个td下的 第2个a节点
//*/td[7]/a[1]

定位 第8个td下的 第3个span节点

//*/td[7]/span[2]

定位 最后一个td下的 最后一个a节点

//*/td[last()]/a[last()]
</pre>

三、使用属性

为了让定位更精准,跟使用索引类似,我们要增加信息量,那么还可以使用属性。@符号是属性符

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#定位所有包含name属性的input节点
//input[@name]

定位含有属性的所有的input节点

//input[@*]

定位所有value=2的input节点

//input[@value='2']

使用多个属性定位

//input[@value='2'][@id='3']
或者//input[@value='2' and @id='3']
</pre>

四、常用函数

除了索引、属性外,Xpath还可以使用便捷的函数来增强定位的准确性。下面试常用的几个函数:

函数含义contains(s1,s2)如果s1中包含s2,返回true;反之,返回falsetext()获取节点中的文本内容starts-with()从其实位置匹配字符串

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><a class="menu_hot" href="/ads/auth/promote.html">应用推广</a>

定位href属性中包含“promote.html”的所有a节点

//a[contains(@href,'promote.html')]

元素内的文本为“应用推广”的所有a节点

//a[text()='应用推广']

href属性值是以“/ads”开头的所有a节点

//a[starts-with(@href,'/ads')]
</pre>

五、Xpath轴

这部分类似BeautifulSoup中的sibling、parents、children方法。 有时候为了实现定位,要拐弯抹角,七大姑八大姨远房亲戚绕来绕去就认识了,就定位到了。

轴名称含义ancestor选取当前节点的所有先辈(父、祖父等)ancestor-or-self选取当前节点的所有先辈(父、祖父等)及当前节点自己attribute选取当前节点的所有属性child选取当前节点的所有子节点descendant选取当前节点的所有后代子节点(子、孙等)descendant-or-self选取当前节点的所有后代子节点(子、孙等)及当前节点自己following选取当前节点结束后的所有节点parent选取当前节点的父节点preceding-sibling选取当前节点之前的所有同级别节点self选取当前节点

六、美团商户信息

代码里我提供了一个高德api_key,但是如果大家用的多了,彼此实际上就无法使用,建议大家自己申请一个。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#在高德注册,进入控制台
http://lbs.amap.com/
</pre>

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1560064732946" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1560064732950" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

这是几十秒中采集的郑州市的数据。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1560064732957" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

tips:

修改高德api_key

在项目中找到settings,修改GAODEAPIKEY参数为自己申请号的api_key。

更改城市

在项目中找到settings,修改CITY_NAME参数为自己想要采集的城市

运行主程序采集数据

项目文件夹找到main.py运行就可以愉快的获取数据,保存到data.csv中。

相关文章

网友评论

    本文标题:美团商家信息采集神器 !Python采集所有商家信息!

    本文链接:https://www.haomeiwen.com/subject/phknxctx.html