# Python 爬虫快速学习


爬虫

概念

在这里插入图片描述

工具

可以爬虫的语言有很多。java、python、c++等。在这里使用python作为工具。

urllib库

urllib库介绍

python内置的urilib库,这个库可以模仿浏览器向服务器发送请求,获取网页内容。

库的重要属性

  • error:异常类
  • Parse:解析Url
  • Request:用各种协议打开URLs的一个扩展库
  • Response:Urilib使用的response
Request的使用
  • request.urlopen()方法使用
# 导入urllib库:完成向服务器发送请求
import urllib.request
response = urllib.request.urlopen("http://www.baidu.com")
result = response.read()
print(result)

result的内容就是我们请求的地址的网页内容

  • urlopen()方法,源码如下
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
            *, cafile=None, capath=None, cadefault=False, context=None):

url:输入的网址

data:给服务器请求的一些附加信息,比如一些登录信息什么的。

timeout:定义超时时间

cafile:CA认证 (很少使用,默认即可)

capath:用于CA认证(很少使用,默认即可)

context:设置SSL加密传输(很少使用,默认即可)

  • response.getinfo():返回Http的header信息
  • response.getcode():返回请求的状态码
Request类
  • 源码中的初始化函数
def __init__(self, url, data=None, headers={},
                 origin_req_host=None, unverifiable=False,
                 method=None):
  • url,data和上面urlopen中的提到的一样。
  • headers是HTTP请求的报文信息,如User_Agent参数等,它可以让爬虫伪装成浏览器而不被服务器发现你正在使用爬虫。headers很有用,可以解决反爬虫机制。
Request使用

结果和前面urlopen是一样的,前面提到urlopen除了可以接受指定参数,也可以接受Request类的对象。’ '里面填写自己浏览器的信息即可。

import urllib.request
headers ={'User-Agent':''}
response = urllib.request.Request('http://www.baidu.com', headers=headers)
html = urllib.request.urlopen(response)
result=html.read()
print(result)
Error的使用
  • UrlError
  • HttpError
    以下代码使用了try…exception的结构,实现了简单的网页爬取,当有异常时,如URLError发生时,就会返回reason,或者HTTPError发生错误时就会返回code。
import urllib.request
import urllib.error
try:
    headers = {'User_Agent': 'Mozilla/5.0 (X11; Ubuntu;Linux x86_64;rv: 57.0) Gecko / 20100101Firefox / 57.0'}
    response = urllib.request.Request('http://blog.csdn.net/qq_37248504/article/details/106891181', headers=headers)
    html = urllib.request.urlopen(response)
    result=html.read()
    print(result)
except urllib.error.HTTPError as e:
    if hasattr(e,'reason'):
        print('错误原因是'+str(e.reason))
except urllib.error.URLError as e:
    if hasattr(e,'code'):
        print('错误状态吗是'+str(e.code))
else:
    print('请求成功!')

正则表达式

概念

用来检索、替换那些符合某个模式(规则)的文本
在这里插入图片描述

爬虫主要思路
  • 先用正则语法定义一个规则
  • 使用这个规则和网页进行对比,根据规则提取内容。
正则表达式例子

例如:一个字符串中包含一个邮箱lidonglidong@qq.com

pattern:\w+@\w+\.com \w+@(\w+\.)?\w+\.com

re模块核心函数
compile()函数
  • 作用:返回一个正则表达式对象
match()函数
  • 作用:从字符串的最开始和pattern进行匹配,匹配成功返回匹配对象,否则返回None

爬取图片简单例子

  • strp():去除字符串两边的空格
urlretrieve方法

从远端下载文件内容,源码如下:

def urlretrieve(url, filename=None, reporthook=None, data=None):
# url:远端文件地址
# filename:本地文件的地址
# reportbook:我们可以利用这个回调函数来显示当前的下载进度
# data: 指 post 到服务器的数据

在这里插入图片描述

re.findall方法

返回string中所有与pattern相匹配的全部字串,返回形式为数组

def findall(pattern, string, flags=0):

在这里插入图片描述

文件解码
import chardet 
result = getHtml("http://pic.yxdown.com/list/0_74_1.html#")
encode_type = chardet.detect(result)
result = result.decode(encode_type['encoding'])  # 进行相应解码,赋给原标识符(变量
  • 完整的Demo
import urllib.request
import re
import urllib
import chardet  # 需要导入这个模块,检测编码格式

得到网页内容

def getHtml(url):
    print("请求网页内容开始==================")
    try:
        print("爬取的网站地址为==============" + url)
        html = urllib.request.urlopen(url)
        result = html.read()
    except urllib.error.HTTPError as e:
        if hasattr(e, 'reason'):
            print('错误原因是' + str(e.reason))
    except urllib.error.URLError as e:
        if hasattr(e, 'code'):
            print('错误状态吗是' + str(e.code))
    else:
        print('请求成功!')
    return result
    print("请求网页内容结束======================")
# 进行内容解码不然会报错
result = getHtml("http://pic.yxdown.com/list/0_74_1.html#")
encode_type = chardet.detect(result)
result = result.decode(encode_type['encoding'])  # 进行相应解码,赋给原标识符(变量
# 展示下载的进度
def cbk(a, b, c):
    '''''回调函数
    @a:已经下载的数据块
    @b:数据块的大小
    @c:远程文件的大小
    '''
    per = 100.0 * a * b / c
    if per > 100:
        per = 100
    print("下载进度为=======================" + '%.2f%%' % per)
# 下载文件中的图片
def getImage(html):
    print("下载图片开始====================")
    reg = 'src="(http:.+?\.jpg)" alt='
    cp = re.compile(reg)
    print(cp)
    # print(html)
    images = re.findall(reg, html)
    x = 0
    for img in images:
        print(x)
        urllib.request.urlretrieve(img, './images/%s.jpg' % x, cbk)
        x += 1
    return images
    print("下载图片结束====================")
# 将结果保存到txt中
# 处理保存的文本文件的格式
def clearBlank(result_content):
    print("处理文本信息开始======================")
    with open('./one.txt', 'wb') as f:
        # 将内容保存到txt中
        f.write(result_content)
        print("保存文件完成==================")
        # 创建文件操作流对象
        file1 = open('./one.txt', 'r', encoding='utf-8')
        file2 = open('./two.txt', 'w', encoding='utf-8')
        try:
            for line in file1.readlines():
                if line == '\n':
                    line = line.strip("\n")
                file2.write(line)
        finally:
            f.close()
            file1.close()
            file2.close()
            print("处理文本信息结束=======================")
if __name__ == '__main__':
    getImage(result)
    clearBlank(getHtml("http://pic.yxdown.com/list/0_74_1.html#"))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值