• python 爬虫基础


    一、什么是爬虫?

    网页上面采集数据

    二、学习爬虫有什么作用?

    做案例分析,做数据分析,分析网页结构.......

    三、爬虫环境

    需求:python3x  pycharm

    模块:urllib 、urllib2、bs4、re

    四、爬虫思路:

    1.      打开网页,获取源码。

    *由于多人同时爬虫某个网站时候,会造成数据冗余,网站崩溃,所以一些网站是禁止爬虫的,会返回403拒绝访问的错误信息。----获取不到想要的内容/请求失败/IP容易被封……..等

    *解决办法:伪装——不告诉网站我是一个脚本,告诉它我是一个浏览器。(加上随便一个浏览器的头部信息,伪装成浏览器),由于是简单例子,那我们就不搞这些骚操作了。

    2.     获取图片

    *Find函数 :只去找第一个目标,查询一次

    *Find_all函数: 找到所有的相同的目标。

    这里可能有一个解析器的问题,我们也不说了,出问题的同学百度一堆解决办法。

    3.      保存图片地址与下载图片

    *a.使用urlib---urlretrieve下载(保存位置:如果保存在跟*.py文件同一个地方,那么只需要文件夹名称即可,如果是其他地方,那么得写绝对路径。)

    算了,不说那么多废话,既然是个简单例子,那我就直接贴代码吧。相信也没多少人呢看不懂。

    提一句:用BeautifulSoup 就可以不用正则;爬虫用正则,Bs4, xpath三种 选择一个就好。当然也可以混合使用,也还有其他种。

    爬取地址:http://tieba.baidu.com/p/3840085725

    import urllib.request
    import re
    import os
    import urllib
    
    #根据给定的网址来获取网页详细信息,得到的html就是网页的源代码
    def getHtml(url):
        page = urllib.request.urlopen(url)
        html = page.read()
        return html.decode('UTF-8')
    
    def getImg(html):
        reg = r'src="(.+?.jpg)" pic_ext'
        imgre = re.compile(reg)                              #转换成一个正则对象
        imglist = imgre.findall(html)                        #表示在整个网页中过滤出所有图片的地址,放在imglist中
    
        x = 0                                                #声明一个变量赋值
        path = 'D:\pythonlianxi\zout_pc5\test'           #设置保存地址
    
    
        if not os.path.isdir(path):
            os.makedirs(path)                                          # 将图片保存到H:..\test文件夹中,如果没有test文件夹则创建
        paths = path+'\'                                              #保存在test路径下
    
        for imgurl in imglist:
    
            urllib.request.urlretrieve(imgurl,'{0}{1}.jpg'.format(paths,x))               #打开imglist,下载图片保存在本地,format格式化字符串
            x = x + 1
            print('图片已开始下载,注意查看文件夹')
        return imglist
    
    html = getHtml("http://tieba.baidu.com/p/3840085725")         #获取该网址网页详细信息,html就是网页的源代码
    print (getImg(html))                                          #从网页源代码中分析并下载保存图片


    https://blog.csdn.net/itbiggod/article/details/78289865

    BeautifulSoup库的使用
    一个灵活又方便的网页解析库,处理高效,支持多种解析器。

    pip install beautifulsoup4
    https://blog.csdn.net/sinat_34937826/article/details/79992728
    pip3 install lxml
    https://blog.csdn.net/qq_34215281/article/details/77714584

    from bs4 import BeautifulSoup
    
    def bshtml():
        html = '''
        <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>
        '''
        soup = BeautifulSoup(html,'lxml')
        print(soup.prettify())
        print(soup.title)
        print(soup.title.name)
        print(soup.title.string)
        print(soup.title.parent.name)
        print(soup.p)
        print(soup.p["class"])
        print(soup.a)
        print(soup.find_all('a'))
        print(soup.find(id='link3'))
    
    bshtml()

    https://www.cnblogs.com/zhaof/p/6930955.html

    中文乱码问题gb2312 --> utf-8
    https://blog.csdn.net/after_you/article/details/70256438


    扒视频
    https://www.cnblogs.com/sss4/p/7809821.html

    扒电商价格
    https://blog.csdn.net/blueheart20/article/details/81184058

    遇到问题解决
    https://blog.csdn.net/weixin_42057852/article/details/80857948

    关于爬取数据保存到json文件,中文是unicode解决方式
    https://www.cnblogs.com/yuyang26/p/7813097.html

  • 相关阅读:
    dedecms内容管理系统使用心得
    flex>样式和主题 小强斋
    flex>样式和主题 小强斋
    flex>HttpService 小强斋
    Flex>连接WebService 小强斋
    flex>HttpService 小强斋
    Struts2>Cannot find the tag library descriptor for /strutstags 小强斋
    flex>HttpService 小强斋
    Flex>连接WebService 小强斋
    Flex>连接WebService 小强斋
  • 原文地址:https://www.cnblogs.com/lely/p/10364374.html
Copyright © 2020-2023  润新知