利用Python爬虫买车比价,自动采集某车之家各车型裸车价

在一位朋友的要求下,帮助收集一家汽车屋的一些汽车品牌销售数据,包括购车时间、车型、经销商、裸车价格等信息。

[En]

At the request of a friend, help collect some car brand sales data of a car house, including car purchase time, model, dealer, naked car price and other information.

今天我们将简要演示采集流程,您可以根据自己的兴趣进行扩展,比如采集您喜欢的品牌汽车数据进行统计分析等。

[En]

Today we will briefly demonstrate the collection process, you can expand according to your own interests, such as collecting your favorite brand car data for statistical analysis and so on.

进入正文:

  1. 目标网页分析
许多人向蟒蛇学习,不知道从哪里开始。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Many people learn from pythons and don't know where to start.</font>*</details>

很多人学习寻找python,掌握了基本语法之后,不知道在哪里案例上手。

许多可能已经了解此案的人并没有学习到更高级的知识。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Many people who may already know the case do not learn more advanced knowledge.</font>*</details>

这三类人,我为你提供了一个很好的学习平台,免费获取视频教程、电子书以及课程源代码!<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>These three categories of people, I provide you with a good learning platform, free access to video tutorials, e-books, as well as course source code!</font>*</details>

QQ群:101677771

欢迎加入,一起讨论学习

目标网站是某车之家关于品牌汽车车型的口碑模块相关数据,比如我们演示的案例奥迪Q5L的口碑页面如下:

https:<span class="hljs-regexp">//k.autohome.com.cn<span class="hljs-regexp">/4851/</span></span>

为了进行演示,您可以直接打开上面的URL,然后拖动到所有的口碑位置,找到我们需要的这个集合的字段,如下所示:

[En]

In order to demonstrate, you can directly open the above URL, and then drag to all the word-of-mouth locations to find the fields we need for this collection as shown below:

采集字段

我们翻了一页,发现浏览器的URL已经改变。您可以了解以下页面的规则:

[En]

We turned the page and found that the browser URL had changed. You can find out the rules for the following pages:

<span class="hljs-attribute">https:</span>

对于上面写网址,我们发现可变部分是车型(如4851)以及页码(如2,3,4),于是我们可以构建url参数如下:

  1. 数据请求

通过一个简单的测试,发现似乎没有防爬行,这很简单。

[En]

Through a simple test, it is found that there seems to be no anti-crawling, which is simple.

让我们首先介绍一下需要使用的库:

[En]

Let’s first introduce the libraries that need to be used:

<span class="hljs-keyword">import requests
<span class="hljs-keyword">import pandas as pd
<span class="hljs-keyword">import html
from lxml <span class="hljs-keyword">import etree
<span class="hljs-keyword">import re
</span></span></span></span></span>

然后为数据请求创建函数备份:

[En]

Then create a function backup for the data request:

请求来的数据就是网页html文本,我们接下来采用re解析出一共多少页码,再用xpath进行采集字段的解析。

  1. 数据解析

由于需要进行翻页,这里我们可以先通过re正则表达式获取总页码。通过查看网页数据,我们发现总页码可以通过如下方式获取:

<span class="hljs-keyword">try:
    pages = int(re.findall(<span class="hljs-string">r'&#x5171;(\d+)&#x9875;',r)[<span class="hljs-number">0])
</span></span></span>

总页码采集

关于待采集字段信息,我们发现都在节点div[@class=”mouthcon-cont-left”]里,可以先定位这个节点数据,然后再进行逐一解析。

待采集字段信息所在节点

此外,我们发现每页最多有15个车辆口碑数据,所以我们可以定位每页需要收集的15个信息数据集,并遍历收集代码:

[En]

In addition, we find that there are up to 15 vehicle word-of-mouth data per page, so we can locate 15 information data sets to be collected per page and traverse the collection code:

<span class="hljs-attribute">divs = r_html.xpath(<span class="hljs-string">'.//div[<span class="hljs-variable">@class="mouthcon-cont-left"]')
</span></span></span>
  1. 数据存储

由于没啥反爬,这里直接将采集到的数据转化为pandas.DataFrame类型,然后存储为xlsx文件即可。

df = pd.DataFrame(items)
df = df[[<span class="hljs-string">'&#x8D2D;&#x4E70;&#x8F66;&#x578B;', <span class="hljs-string">'&#x8D2D;&#x4E70;&#x914D;&#x7F6E;', <span class="hljs-string">'&#x8D2D;&#x4E70;&#x5730;&#x70B9;', <span class="hljs-string">'&#x8D2D;&#x8F66;&#x7ECF;&#x9500;&#x5546;', <span class="hljs-string">'&#x8D2D;&#x4E70;&#x65F6;&#x95F4;', <span class="hljs-string">'&#x88F8;&#x8F66;&#x8D2D;&#x4E70;&#x4EF7;']]
</span></span></span></span></span></span>
  1. 采集结果预览

整个爬虫过程比较简单,采集下来的数据也比较规范,以本文案例奥迪Q5L示例如下:

采集结果预览

这些都是这次的内容,比较简单。感兴趣的学生可以在此基础上收集一些有趣的数据,并尝试进行统计分析、可视化展示等。

[En]

These are all the contents of this time, which is relatively simple. Interested students can collect some interesting data based on this and try to do statistical analysis, visual display and so on.

Original: https://www.cnblogs.com/sn5200/p/15802187.html
Author: Python可乐的呀
Title: 利用Python爬虫买车比价,自动采集某车之家各车型裸车价

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/510252/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球