资讯专栏INFORMATION COLUMN

Python 爬虫实战(一):使用 requests 和 BeautifulSoup

jokester / 1616人阅读

摘要:建立连接插入数据使用方法创建一个游标对象执行语句提交事务已经存在如果发生错误则回滚关闭游标连接关闭数据库连接定时设置做了一个定时,过段时间就去爬一次。

Python 基础

我之前写的《Python 3 极简教程.pdf》,适合有点编程基础的快速入门,通过该系列文章学习,能够独立完成接口的编写,写写小东西没问题。

requests

requests,Python HTTP 请求库,相当于 Android 的 Retrofit,它的功能包括 Keep-Alive 和连接池、Cookie 持久化、内容自动解压、HTTP 代理、SSL 认证、连接超时、Session 等很多特性,同时兼容 Python2 和 Python3,GitHub:https://github.com/requests/r... 。

安装

Mac:

pip3 install requests

Windows:

pip install requests
发送请求

HTTP 请求方法有 get、post、put、delete。

import requests

# get 请求
response = requests.get("http://127.0.0.1:1024/developer/api/v1.0/all")

# post 请求
response = requests.post("http://127.0.0.1:1024/developer/api/v1.0/insert")

# put 请求
response = requests.put("http://127.0.0.1:1024/developer/api/v1.0/update")

# delete 请求
response = requests.delete("http://127.0.0.1:1024/developer/api/v1.0/delete")

请求返回 Response 对象,Response 对象是对 HTTP 协议中服务端返回给浏览器的响应数据的封装,响应的中的主要元素包括:状态码、原因短语、响应首部、响应 URL、响应 encoding、响应体等等。

# 状态码
print(response.status_code)

# 响应 URL
print(response.url)

# 响应短语
print(response.reason)

# 响应内容
print(response.json())
定制请求头

请求添加 HTTP 头部 Headers,只要传递一个 dict 给 headers 关键字参数就可以了。

header = {"Application-Id": "19869a66c6",
          "Content-Type": "application/json"
          }
response = requests.get("http://127.0.0.1:1024/developer/api/v1.0/all/", headers=header)
构建查询参数

想为 URL 的查询字符串(query string)传递某种数据,比如:http://127.0.0.1:1024/developer/api/v1.0/all?key1=value1&key2=value2 ,Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数。

payload = {"key1": "value1", "key2": "value2"}
response = requests.get("http://127.0.0.1:1024/developer/api/v1.0/all", params=payload)

还可以将 list 作为值传入:

payload = {"key1": "value1", "key2": ["value2", "value3"]}
response = requests.get("http://127.0.0.1:1024/developer/api/v1.0/all", params=payload)

# 响应 URL
print(response.url)# 打印:http://127.0.0.1:1024/developer/api/v1.0/all?key1=value1&key2=value2&key2=value3
post 请求数据

如果服务器要求发送的数据是表单数据,则可以指定关键字参数 data。

payload = {"key1": "value1", "key2": "value2"}
response = requests.post("http://127.0.0.1:1024/developer/api/v1.0/insert", data=payload)

如果要求传递 json 格式字符串参数,则可以使用 json 关键字参数,参数的值都可以字典的形式传过去。

obj = {
    "article_title": "小公务员之死2"
}
# response = requests.post("http://127.0.0.1:1024/developer/api/v1.0/insert", json=obj)
响应内容

Requests 会自动解码来自服务器的内容。大多数 unicode 字符集都能被无缝地解码。请求发出后,Requests 会基于 HTTP 头部对响应的编码作出有根据的推测。

# 响应内容
# 返回是 是 str 类型内容
# print(response.text())
# 返回是 JSON 响应内容
print(response.json())
# 返回是二进制响应内容
# print(response.content())
# 原始响应内容,初始请求中设置了 stream=True
# response = requests.get("http://127.0.0.1:1024/developer/api/v1.0/all", stream=True)
# print(response.raw())
超时

如果没有显式指定了 timeout 值,requests 是不会自动进行超时处理的。如果遇到服务器没有响应的情况时,整个应用程序一直处于阻塞状态而没法处理其他请求。

response = requests.get("http://127.0.0.1:1024/developer/api/v1.0/all", timeout=5)  # 单位秒数
代理设置

如果频繁访问一个网站,很容易被服务器屏蔽掉,requests 完美支持代理。

# 代理
proxies = {
    "http": "http://127.0.0.1:1024",
    "https": "http://127.0.0.1:4000",
}
response = requests.get("http://127.0.0.1:1024/developer/api/v1.0/all", proxies=proxies)
BeautifulSoup

BeautifulSoup,Python Html 解析库,相当于 Java 的 jsoup。

安装

BeautifulSoup 3 目前已经停止开发,直接使用BeautifulSoup 4。

Mac:

pip3 install beautifulsoup4

Windows:

pip install beautifulsoup4
安装解析器

我用的是 html5lib,纯 Python 实现的。

Mac:

pip3 install html5lib

Windows:

pip install html5lib
简单使用

BeautifulSoup 将复杂 HTML 文档转换成一个复杂的树形结构,每个节点都是 Python 对象。

解析
from bs4 import BeautifulSoup

def get_html_data():
    html_doc = """
    
    
    WuXiaolong
    
    
    

分享 Android 技术,也关注 Python 等热门技术。

写博客的初衷:总结经验,记录自己的成长。

你必须足够的努力,才能看起来毫不费力!专注!精致!

WuXiaolong"s blog

公众号:吴小龙同学

GitHub

""" soup = BeautifulSoup(html_doc, "html5lib")
tag
tag = soup.head
print(tag)  # WuXiaolong
print(tag.name)  # head
print(tag.title)  # WuXiaolong
print(soup.p)  # 

分享 Android 技术,也关注 Python 等热门技术。

print(soup.a["href"]) # 输出 a 标签的 href 属性:http://wuxiaolong.me/

注意:tag 如果多个匹配,返回第一个,比如这里的 p 标签。

查找
print(soup.find("p"))  # 

分享 Android 技术,也关注 Python 等热门技术。

find 默认也是返回第一个匹配的标签,没找到匹配的节点则返回 None。如果我想指定查找,比如这里的公众号,可以指定标签的如 class 属性值:

# 因为 class 是 Python 关键字,所以这里指定为 class_。
print(soup.find("p", class_="WeChat"))
# 

公众号

查找所有的 P 标签:

for p in soup.find_all("p"):
    print(p.string) 
实战

前段时间,有用户反馈,我的个人 APP 挂了,虽然这个 APP 我已经不再维护,但是我也得起码保证它能正常运行。大部分人都知道这个 APP 数据是爬来的(详见:《手把手教你做个人app》),数据爬来的好处之一就是不用自己管数据,弊端是别人网站挂了或网站的 HTML 节点变了,我这边就解析不到,就没数据。这次用户反馈,我在想要不要把他们网站数据直接爬虫了,正好自学 Python,练练手,嗯说干就干,本来是想着先用 Python 爬虫,MySQL 插入本地数据库,然后 Flask 自己写接口,用 Android 的 Retrofit 调,再用 bmob sdk 插入 bmob……哎,费劲,感觉行不通,后来我得知 bmob 提供了 RESTful,解决大问题,我可以直接 Python 爬虫插入就好了,这里我演示的是插入本地数据库,如果用 bmob,是调 bmob 提供的 RESTful 插数据。

网站选定

我选的演示网站:https://meiriyiwen.com/random ,大家可以发现,每次请求的文章都不一样,正好利用这点,我只要定时去请求,解析自己需要的数据,插入数据库就 OK 了。

创建数据库

我直接用 NaviCat Premium 创建的,当然也可以用命令行。

创建表

创建表 article,用的 pymysql,表需要 id,article_title,article_author,article_content 字段,代码如下,只需要调一次就好了。

import pymysql


def create_table():
    # 建立连接
    db = pymysql.connect(host="localhost",
                         user="root",
                         password="root",
                         db="python3learn")
    # 创建名为 article 数据库语句
    sql = """create table if not exists article (
    id int NOT NULL AUTO_INCREMENT, 
    article_title text,
    article_author text,
    article_content text,
    PRIMARY KEY (`id`)
    )"""
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:
        # 执行 sql 语句
        cursor.execute(sql)
        # 提交事务
        db.commit()
        print("create table success")
    except BaseException as e:  # 如果发生错误则回滚
        db.rollback()
        print(e)

    finally:
        # 关闭游标连接
        cursor.close()
        # 关闭数据库连接
        db.close()


if __name__ == "__main__":
    create_table()
解析网站

首先需要 requests 请求网站,然后 BeautifulSoup 解析自己需要的节点。

import requests
from bs4 import BeautifulSoup


def get_html_data():
    # get 请求
    response = requests.get("https://meiriyiwen.com/random")

    soup = BeautifulSoup(response.content, "html5lib")
    article = soup.find("div", id="article_show")
    article_title = article.h1.string
    print("article_title=%s" % article_title)
    article_author = article.find("p", class_="article_author").string
    print("article_author=%s" % article.find("p", class_="article_author").string)
    article_contents = article.find("div", class_="article_text").find_all("p")
    article_content = ""
    for content in article_contents:
        article_content = article_content + str(content)
        print("article_content=%s" % article_content)
插入数据库

这里做了一个筛选,默认这个网站的文章标题是唯一的,插入数据时,如果有了同样的标题就不插入。

import pymysql


def insert_table(article_title, article_author, article_content):
    # 建立连接
    db = pymysql.connect(host="localhost",
                         user="root",
                         password="root",
                         db="python3learn",
                         charset="utf8")
    # 插入数据
    query_sql = "select * from article where article_title=%s"
    sql = "insert into article (article_title,article_author,article_content) values (%s, %s, %s)"
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:
        query_value = (article_title,)
        # 执行 sql 语句
        cursor.execute(query_sql, query_value)
        results = cursor.fetchall()
        if len(results) == 0:
            value = (article_title, article_author, article_content)
            cursor.execute(sql, value)
            # 提交事务
            db.commit()
            print("--------------《%s》 insert table success-------------" % article_title)
            return True
        else:
            print("--------------《%s》 已经存在-------------" % article_title)
            return False

    except BaseException as e:  # 如果发生错误则回滚
        db.rollback()
        print(e)

    finally:  # 关闭游标连接
        cursor.close()
        # 关闭数据库连接
        db.close()
定时设置

做了一个定时,过段时间就去爬一次。

import sched
import time


# 初始化 sched 模块的 scheduler 类
# 第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。
schedule = sched.scheduler(time.time, time.sleep)


# 被周期性调度触发的函数
def print_time(inc):
    # to do something
    print("to do something")
    schedule.enter(inc, 0, print_time, (inc,))


# 默认参数 60 s
def start(inc=60):
    # enter四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,
    # 给该触发函数的参数(tuple形式)
    schedule.enter(0, 0, print_time, (inc,))
    schedule.run()


if __name__ == "__main__":
    # 5 s 输出一次
    start(5)
完整代码
import pymysql
import requests
from bs4 import BeautifulSoup
import sched
import time


def create_table():
    # 建立连接
    db = pymysql.connect(host="localhost",
                         user="root",
                         password="root",
                         db="python3learn")
    # 创建名为 article 数据库语句
    sql = """create table if not exists article (
    id int NOT NULL AUTO_INCREMENT, 
    article_title text,
    article_author text,
    article_content text,
    PRIMARY KEY (`id`)
    )"""
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:
        # 执行 sql 语句
        cursor.execute(sql)
        # 提交事务
        db.commit()
        print("create table success")
    except BaseException as e:  # 如果发生错误则回滚
        db.rollback()
        print(e)

    finally:
        # 关闭游标连接
        cursor.close()
        # 关闭数据库连接
        db.close()


def insert_table(article_title, article_author, article_content):
    # 建立连接
    db = pymysql.connect(host="localhost",
                         user="root",
                         password="root",
                         db="python3learn",
                         charset="utf8")
    # 插入数据
    query_sql = "select * from article where article_title=%s"
    sql = "insert into article (article_title,article_author,article_content) values (%s, %s, %s)"
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:
        query_value = (article_title,)
        # 执行 sql 语句
        cursor.execute(query_sql, query_value)
        results = cursor.fetchall()
        if len(results) == 0:
            value = (article_title, article_author, article_content)
            cursor.execute(sql, value)
            # 提交事务
            db.commit()
            print("--------------《%s》 insert table success-------------" % article_title)
            return True
        else:
            print("--------------《%s》 已经存在-------------" % article_title)
            return False

    except BaseException as e:  # 如果发生错误则回滚
        db.rollback()
        print(e)

    finally:  # 关闭游标连接
        cursor.close()
        # 关闭数据库连接
        db.close()


def get_html_data():
    # get 请求
    response = requests.get("https://meiriyiwen.com/random")

    soup = BeautifulSoup(response.content, "html5lib")
    article = soup.find("div", id="article_show")
    article_title = article.h1.string
    print("article_title=%s" % article_title)
    article_author = article.find("p", class_="article_author").string
    print("article_author=%s" % article.find("p", class_="article_author").string)
    article_contents = article.find("div", class_="article_text").find_all("p")
    article_content = ""
    for content in article_contents:
        article_content = article_content + str(content)
        print("article_content=%s" % article_content)

    # 插入数据库
    insert_table(article_title, article_author, article_content)


# 初始化 sched 模块的 scheduler 类
# 第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。
schedule = sched.scheduler(time.time, time.sleep)


# 被周期性调度触发的函数
def print_time(inc):
    get_html_data()
    schedule.enter(inc, 0, print_time, (inc,))


# 默认参数 60 s
def start(inc=60):
    # enter四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,
    # 给该触发函数的参数(tuple形式)
    schedule.enter(0, 0, print_time, (inc,))
    schedule.run()


if __name__ == "__main__":
    start(60*5)

问题:这只是对一篇文章爬虫,如果是那种文章列表,点击是文章详情,这种如何爬虫解析?首先肯定要拿到列表,再循环一个个解析文章详情插入数据库?还没有想好该如何做更好,留给后面的课题吧。

最后

虽然我学 Python 纯属业余爱好,但是也要学以致用,不然这些知识很快就忘记了,期待下篇 Python 方面的文章。

参考

快速上手 — Requests 2.18.1 文档

爬虫入门系列(二):优雅的HTTP库requests

Beautiful Soup 4.2.0 文档

爬虫入门系列(四):HTML文本解析库BeautifulSoup

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41082.html

相关文章

  • Python 从零开始爬虫(三)——实战requests+BeautifulSoup实现静态爬取

    摘要:前篇全片都是生硬的理论使用,今天就放个静态爬取的实例让大家体验一下的使用,了解一些背后的原理。给出网站打开右键检查第一个电影,分析源码先,发现每个标签就对应着一个电影的信息。 前篇全片都是生硬的理论使用,今天就放个静态爬取的实例让大家体验一下BeautifulSoup的使用,了解一些背后的原理。 顺便在这引入静态网页的概念——静态网页是指一次性加载所有内容的网页,爬虫一次请求便能得到所...

    Codeing_ls 评论0 收藏0
  • Python 爬虫实战(二):使用 requests-html

    摘要:爬虫实战一使用和,我们使用了做网络请求,拿到网页数据再用解析,就在前不久,作者出了一个新库,,它可以用于解析文档的。是基于现有的框架等库进行了二次封装,更加方便开发者调用。参考今天用了一下库爬虫公众号我的公众号吴小龙同学,欢迎交流 Python 爬虫实战(一):使用 requests 和 BeautifulSoup,我们使用了 requests 做网络请求,拿到网页数据再用 Beaut...

    honmaple 评论0 收藏0
  • Python爬虫基础

    摘要:爬虫架构架构组成管理器管理待爬取的集合和已爬取的集合,传送待爬取的给网页下载器。网页下载器爬取对应的网页,存储成字符串,传送给网页解析器。从文档中获取所有文字内容正则匹配后记爬虫基础知识,至此足够,接下来,在实战中学习更高级的知识。 前言 Python非常适合用来开发网页爬虫,理由如下:1、抓取网页本身的接口相比与其他静态编程语言,如java,c#,c++,python抓取网页文档的接...

    bang590 评论0 收藏0
  • 爬虫到机器学习预测,我是如何步做到的?

    摘要:数据分析实战北京二手房房价分析数据分析实战北京二手房房价分析建模篇除了数据分析,好多朋友也对爬虫特别感兴趣,想知道爬虫部分是如何实现的。爬取目标是北京二手房,仅针对一个城市而言,数据量并不大。 作者:xiaoyu 微信公众号:Python数据科学 知乎:python数据分析师 showImg(https://segmentfault.com/img/remote/1460000016...

    Tamic 评论0 收藏0
  • python爬虫实战:爬取西刺代理的代理ip(二)

    摘要:爬虫实战二爬取西刺代理的代理对于刚入门的同学来说,本次实战稍微有点难度,但是简单的爬取图片文本之类的又没营养,所以这次我选择了爬取西刺代理的地址,爬取的代理也能在以后的学习中用到本次实战用的主要知识很多,其中包括自动保存利用抓包工具获取和匹 爬虫实战(二):爬取西刺代理的代理ip 对于刚入门的同学来说,本次实战稍微有点难度,但是简单的爬取图片、文本之类的又没营养,所以这次我选择了爬...

    fsmStudy 评论0 收藏0

发表评论

0条评论

jokester

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<