zoukankan      html  css  js  c++  java
  • python网络爬虫之爬取图片

    今天使用requests和BeautifulSoup爬取了一些图片,还是很有成就感的,注释可能有误,希望大家多提意见:

    方法一:requests

    import requests
    from bs4 import BeautifulSoup
    circle = requests.get('http://travel.quanjing.com/tag/12975/%E9%A9%AC%E5%B0%94%E4%BB%A3%E5%A4%AB')
    
    # 将获取的图片地址依次放入count中
    count = []
    # 将获取的网页内容放入BeautifulSoup
    soup = BeautifulSoup(circle.text, 'lxml')
    # 根据谷歌SelectGadGet这个插件,获取html标签,比如获取:#gallery-list
    for item in soup.select('#gallery-list'):
        # 用bs4中的find_all获取 #gallery-list 中是否存在 img这个标签
        for img in item.find_all('img'):
            print('img', img)
            # m 是 img标签中存在的属性
            img_path = img.get('m')
            count.append(img_path)
    # 用enumerate依次取出count中的图片地址 放入v中
    for i,v in enumerate(count):
        # 将获取的v值再次放入request中进行与网站相应
        image = requests.get(v)
        # 存取图片过程中,出现不能存储 int 类型,故而,我们对他进行类型转换 str()。w:读写方式打开,b:二进制进行读写。图片一般用到的都是二进制。
        with open('D:\img'+str(i)+'.jpg', 'wb') as file:
            # content:图片转换成二进制,进行保存。
            file.write(image.content)
        print(i)

     方法二:urllib.request

    from bs4 import BeautifulSoup
    import urllib.request as ure
    import os
    url = 'http://travel.quanjing.com/tag/12975/%E9%A9%AC%E5%B0%94%E4%BB%A3%E5%A4%AB'
    response = ure.urlopen(url, timeout=30)
    circle = response.read().decode('utf-8')
    
    # 将获取的图片地址依次放入count中
    count = []
    # 将获取的网页内容放入BeautifulSoup
    soup = BeautifulSoup(circle, 'lxml')
    # 根据谷歌SelectGadGet这个插件,获取html标签,比如获取:#gallery-list
    for item in soup.select('#gallery-list'):
        # 用bs4中的find_all获取 #gallery-list 中是否存在 img这个标签,limit:限制爬取的数量。
        for img in item.find_all('img', limit=5):
            print('img', img)
            # m 是 img标签中存在的属性
            img_path = img.get('m')
            count.append(img_path)
    if not os.path.exists('photo'):
        os.makedirs('photo')
    # 用enumerate依次取出count中的图片地址 放入v中
    for i,v in enumerate(count):
        # 存取图片过程中,出现不能存储 int 类型,故而,我们对他进行类型转换 str()。w:读写方式打开,b:二进制进行读写。图片一般用到的都是二进制。
        path = 'photo\img'+str(i)+'.jpg'
        with open(path, 'w') as file:
            ure.urlretrieve(v, path)
        print(i)
  • 相关阅读:
    ArcGIS Engine要素渲染和专题图制作(转)
    网络换行符替换为word换行符
    栅格数据处理 RasterDataset RasterLayer Raster RasterBandCollection
    ISurfaceOp 接口生成等高线
    小女孩动画
    pytest 知识点学习
    pytest+python 项目初步实践及生成allure报告
    docker安装python项目
    windows 10 专业版 安装docker desktop
    selenium+python自动化测试--解决无法启动IE浏览器及报错问题
  • 原文地址:https://www.cnblogs.com/peixu/p/8035060.html
Copyright © 2011-2022 走看看