(一)Python3 爬虫基本知识和爬虫常用的库和方法

爬虫基本知识

爬虫概念

爬虫(网络爬虫),是一种按照一定规则自动抓取万维网信息的程序或者脚本。理论上来说,只要是我们在浏览器(客户端)能够做的事情,爬虫都可以做。

网页的特征

1.每一个网页都有一个唯一的url(统一资源定位符),来进行定位
2.网页都是通过HTML(超文本)文本展示的
3.所有的网页都是通过HTTP<超文本传输协议>(HTTPS)协议来传输的

爬虫分类和流程

常用爬虫主要分为两类:
1.通用网络爬虫:通常指对搜索引擎的爬虫。
2.聚焦网络爬虫:即爬取某些特定页面和主题的网络爬虫。

爬虫的基本流程如下图所示:
通用网络爬虫
聚焦网络爬虫
1.分析网站,得到目标url
2.根据url,发起请求,获取页面的HTML源码
3.从页面源码中提取数据
     a.提取到目标数据,做数据的筛选和持久化存储
     b.从页面中提取到新的url地址,继续执行第二步操作
4.爬虫结束:所有的目标url都提取完毕,并且得到数据了,再也没有其他请求任务了,这时意味着爬虫结束。

聚焦爬虫在通用爬虫的基础上,增加了对某些特定页面和主题的定义。在URL的检索分析过程中保留有用的网页和信息,然后再将信息保存。

python 3爬虫常用库安装和使用

请求库

1.requests

安装方式
windows系统下:

pip install requests

linux系统下:

sudo pip install requests

或者去github上安装:
https://github.com/requests/requests

导入模块:

import requests

最常用的方法是requests.get(),具体参数是:

r=requests.get(url,params,**kwargs)

url:爬取的网站地址。
params:参数,url中的额外参数。
**kwargs:12个控制访问的参数。

仅举一小例说明:

具体介绍和高级用法可见:
https://requests.kennethreitz.org//zh_CN/latest/user/quickstart.html
https://blog.csdn.net/pittpakk/article/details/81218566

2.selenium(浏览器自动化测试框架)

安装步骤:

1.安装selenium
Win:

pip install selenium

2.安装webdriver

各大浏览器webdriver地址可参见:https://docs.seleniumhq.org/download/
Firefox:https://github.com/mozilla/geckodriver/releases/
Chrome:https://sites.google.com/a/chromium.org/chromedriver/ 或者
http://chromedriver.storage.googleapis.com/index.html
IE:http://selenium-release.storage.googleapis.com/index.html
注:webdriver需要和对应的浏览器版本以及selenium版本对应

3.添加webdriver.exe路径到环境变量
尤其是出现以下报错的情况:selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

使用方法(仅举例用google webdriver打开浏览器):

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(5)
driver.close()

执行后应该是:在这里插入图片描述
更详细介绍可见:
https://www.jianshu.com/p/1531e12f8852

解析库

Beautiful Soup4(推荐)

安装

pip install beautifulsoup4

要注意,包名是beautifulsoup4,如果不加上 4,会是老版本也就是 bs3,它是为了兼容性而存在,目前已不推荐。

常用方法

引用官网的例子:

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

使用 bs 的初始化操作,是用文本创建一个 BeautifulSoup 对象,建议手动指定解析器:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

注:bs在使用的时候需要指定一个”解析器”(解析器不同使用的效果也不同):
html.parse- python 自带,但容错性不够高,对于一些写得不太规范的网页会丢失部分内容

lxml- 解析速度快,需额外安装

xml- 同属 lxml 库,支持 XML 文档

html5lib- 最好的容错性,但速度稍慢

获取其中某个结构化元素及其属性:

soup.title  # title 元素
# <title>The Dormouse's story</title>

soup.p  # 第一个 p 元素
# <p class="title"><b>The Dormouse's story</b></p>

soup.p['class']  # p 元素的 class 属性
# ['title']

soup.p.b  # p 元素下的 b 元素
# <b>The Dormouse's story</b>

soup.p.parent.name  # p 元素的父节点的标签
# body

其他详细介绍可参看官方文档:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章