美文网首页大数据 爬虫Python AI SqlPython小哥哥
Docker最全教程之Python爬网实战!

Docker最全教程之Python爬网实战!

作者: 14e61d025165 | 来源:发表于2019-04-16 14:45 被阅读0次

Python目前是流行度增长最快的主流编程语言,也是第二大最受开发者喜爱的语言(参考Stack Overflow 2019开发者调查报告发布)。 笔者建议.NET、Java开发人员可以将Python发展为第二语言,一方面Python在某些领域确实非常犀利(爬虫、算法、人工智能等等),另一方面,相信我,Python上手完全没有门槛,你甚至无需购买任何书籍!

由于近期在筹备4.21的长沙开发者大会,耽误了不少时间。不过这次邀请到了腾讯资深技术专家、.NET中国社区领袖,微软MVP张善友;52ABP开源框架的作者,微软MVP梁桐铭;知名技术类作家汪鹏,腾讯高级工程师卓伟,腾讯云高级产品经理胡李伟等等, 有兴趣参加的朋友可以点击公众号菜单【联系我们】==>【报名】进行报名,技术不分语言,亦没有界限,期待和你分享、交流!

目录

如果你在学习Python的过程当中有遇见任何问题,可以加入我的python交流学习qq群:683380553,多多交流问题,互帮互助,群里有不错的学习教程和开发工具。学习python有任何问题(学习方法,学习效率,如何就业),可以随时来咨询我,如果你准备学习大数据,也欢迎加入大数据学习交流qq群683380553,每天与大家分享学习资源哦。

关于Python

官方镜像

使用Python抓取博客列表

需求说明

了解Beautiful Soup

分析并获取抓取规则

编写代码实现抓取逻辑

编写Dockerfile

运行并查看抓取结果

关于Python

Python是一种计算机程序设计语言。是一种动态的、面向对象的脚本语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。 Python目前是流行度增长最快的主流编程语言,也是第二大最受开发者喜爱的语言(参考Stack Overflow 2019开发者调查报告发布) 。

Python是一种解释型脚本语言,可以应用于以下领域:

  • Web 和 Internet开发
  • 科学计算和统计
  • 教育
  • 桌面界面开发
  • 软件开发
  • 后端开发

Python学习起来没有门槛,但是通过它,你可以用更短的时间,更高的效率学习和掌握机器学习,甚至是深度学习的技能。不过单单只会Python对大多数人来说是不行的,你最好还掌握一门静态语言(.NET/Java)。 同时,笔者也建议.NET、Java开发人员可以将Python发展为第二语言,一方面Python在某些领域确实非常犀利(爬虫、算法、人工智能等等),另一方面,相信我,Python上手完全没有门槛,你甚至无需购买任何书籍!

官方镜像

官方镜像地址: https://hub.docker.com/_/python

注意,请认准官方镜像:

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555397046524" 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>

使用Python抓取博客列表

需求说明

本篇使用Python来抓取我的博客园的博客列表,打印出标题、链接、日期和摘要。

博客地址: http://www.cnblogs.com/codelove/

内容如下所示:

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555397046531" 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>

了解Beautiful Soup

Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库,支持多种解析器。Beautiful Soup简单的说,就是一个灵活又方便的网页解析库,是一个爬网利器。本篇教程我们就基于Beautiful Soup来抓取博客数据。

Beautiful Soup官方网站: https://beautifulsoup.readthedocs.io

主要解析器说明:

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555397046536" 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>

分析并获取抓取规则

首先我们使用Chrome浏览器打开以下地址: http://www.cnblogs.com/codelove/

然后按下F12打开开发人员工具,通过工具我们梳理了以下规则:

  • 博客块(div.day)
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555397046543" 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>

  • 博客标题(div. postTitle a)
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555397046545" 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>

  • 其他内容获取,如日期、博客链接、简介,这里我们就不截图了。

然后我们通过观察博客路径,获取到url分页规律:

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555397046548" 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>

根据以上分析,我们胸有成竹,开始编码。

编写代码实现抓取逻辑

在编码前,请阅读BeautifulSoup官方文档。然后根据需求,我们编写Python的代码如下所示:

<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;"># 关于BeautifulSoup,请阅读官方文档:https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/#id52
from bs4 import BeautifulSoup
import os
import sys
import requests
import time
import re
url = "https://www.cnblogs.com/codelove/default.html?page={page}"

已完成的页数序号,初时为0

page = 0
while True:
page += 1
request_url = url.format(page=page)
response = requests.get(request_url)
#使用BeautifulSoup的html5lib解析器解析HTML(兼容性最好)
html = BeautifulSoup(response.text,'html5lib')

#获取当前HTML的所有的博客元素
blog_list = html.select(".forFlow .day")

# 循环在读不到新的博客时结束
if not blog_list:
    break

print("fetch: ", request_url)

for blog in blog_list:
    # 获取标题
    title = blog.select(".postTitle a")[0].string
    print('--------------------------'+title+'--------------------------');

    # 获取博客链接
    blog_url = blog.select(".postTitle a")[0]["href"]
    print(blog_url);

    # 获取博客日期
    date = blog.select(".dayTitle a")[0].get_text()
    print(date)

    # 获取博客简介
    des = blog.select(".postCon > div")[0].get_text()
    print(des)

    print('-------------------------------------------------------------------------------------');

</pre>

如上述代码所示,我们根据分析的规则循环翻页并且从每一页的HTML中抽取出了我们需要的博客信息,并打印出来,相关代码已提供注释,这里我们就不多说了。

编写Dockerfile

代码写完,按照惯例,我们仍然是使用Docker实现本地无SDK开发,因此编写Dockerfile如下所示:

<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;"># 使用官方镜像
FROM python:3.7-slim

设置工作目录

WORKDIR /app

复制当前目录

COPY . /app

安装模块

RUN pip install --trusted-host pypi.python.org -r requirements.txt

Run app.py when the container launches

CMD ["python", "app.py"]
</pre>

注意,由于我们使用到了比如beautifulsoup等第三方库,因此我们需要安装相关模块。requirements.txt内容如下所示(注意换行):

html5lib

beautifulsoup4

requests

运行并查看抓取结果

构建完成后,我们运行起来结果如下所示:

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555397046570" 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>

相关文章

网友评论

    本文标题:Docker最全教程之Python爬网实战!

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