爬虫入门-4-2.爬取豆瓣读书图片

一.利用lxml解析

from lxml import etree
import os
import requests

PROXY = {
    ‘HTTPS‘: ‘116.209.55.208:9999‘
}

def spider():
    url = ‘https://book.douban.com/latest?icn=index-latestbook-all‘
    response = requests.get(url, proxies=PROXY)
    html = etree.HTML(response.content)
    # imgs是list类型
    imgs = html.xpath("//div[@class=‘article‘]//ul/li/a/img/@src")
    save_pic(imgs)

def save_pic(images):
    j = 0
    if os.path.exists(‘pi‘):
        pass
    else:
        os.mkdir(‘pi‘)
    for img in images:
        j = j + 1
        with open(‘pi/‘ + str(j) + ‘.jpg‘, ‘wb‘) as fd:
            picture = requests.get(img).content
            fd.write(picture)

if __name__ == ‘__main__‘:
    spider()

二.利用beautifulSoup解析:


 1 import os
 2
 3 import requests
 4 from bs4 import BeautifulSoup
 5
 6 PROXY = {
 7     ‘HTTPS‘: ‘116.209.55.208:9999‘
 8 }
 9
10
11 def spider():
12     url = ‘https://book.douban.com/latest?icn=index-latestbook-all‘
13     response = requests.get(url, proxies=PROXY)
14     soup = BeautifulSoup(response.content, ‘lxml‘)
15     # class为cover的所有a标签
16     a_tags = soup.find_all("a", class_=‘cover‘)
17     for tag in a_tags:
18         # 查找a标签下的所有img标签
19         imgs = tag.find_all("img")
20         for img in imgs:
21             # 获取所有img标签的src属性,返回的是<class ‘str‘>
22             pics = img.get(‘src‘)
23             filename = pics[-13:]
24             print(filename)
25             with open(‘pic2s/‘ + filename, ‘wb‘) as fd:
26                 picture = requests.get(pics).content
27                 fd.write(picture)
28
29
30 if __name__ == ‘__main__‘:
31     spider()

第一种解析

 1 import requests
 2 from bs4 import BeautifulSoup
 3
 4 PROXY = {
 5     ‘HTTPS‘: ‘116.209.55.208:9999‘
 6 }
 7
 8
 9 def spider():
10     url = ‘https://book.douban.com/latest?icn=index-latestbook-all‘
11     response = requests.get(url, proxies=PROXY)
12     soup = BeautifulSoup(response.content, ‘lxml‘)
13     imgs=soup.select(".cover img")
14     for img in imgs:
15         pics = img.get(‘src‘)
16         filename = pics[-13:]
17         print(filename)
18         with open(‘pic3s/‘ + filename, ‘wb‘) as fd:
19             picture = requests.get(pics).content
20             fd.write(picture)
21
22
23 if __name__ == ‘__main__‘:
24     spider()

第二种解析

三.爬取豆瓣图书图片

 1 import os
 2
 3 import requests
 4 from lxml import etree
 5
 6 j = 0
 7 for i in range(0, 6):
 8     r = requests.get(
 9         ‘https://book.douban.com/tag/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C?‘ + ‘start=%d&type=T‘ % i * 20).content
10     books = etree.HTML(r)
11     imgs = books.xpath(‘//*[@id="subject_list"]/ul/li/div[1]/a/img/@src‘, stream=True)
12     if os.path.exists(‘downloads‘):
13         pass
14     else:
15         os.mkdir(‘downloads‘)
16     for img in imgs:
17         j = j + 1
18         with open(‘downloads/‘+str(j) + ‘.jpg‘, ‘wb‘) as fd:
19             picture = requests.get(img).content
20             fd.write(picture)

原文地址:https://www.cnblogs.com/min-R/p/10506650.html

时间: 2024-08-26 13:06:04

爬虫入门-4-2.爬取豆瓣读书图片的相关文章

第一个nodejs爬虫:爬取豆瓣电影图片

第一个nodejs爬虫:爬取豆瓣电影图片存入本地: 首先在命令行下 npm install request cheerio express -save; 代码: var http = require('https'); //使用https模块 var fs = require('fs');//文件读写 var cheerio = require('cheerio');//jquery写法获取所得页面dom元素 var request = require('request');//发送reques

Python爬虫 爬取豆瓣读书

最近用Python写了个豆瓣读书的爬虫玩,方便搜罗各种美美书,分享给大家 实现功能: 1 可以爬下豆瓣读书标签下的所有图书 2 按评分排名依次存储 3 存储到Excel中,可方便大家筛选搜罗,比如筛选评价人数>1000的高分书籍:可依据不同的主题存储到Excel不同的Sheet 4 采用User Agent伪装为浏览器进行爬取,并加入随机延时来更好的模仿浏览器行为,避免爬虫被封 试着爬了七八万本书,存在了book_list.xlsx中,结果截图如下: 详细代码和爬取的一些结果可移步到GitHub

Python3 爬虫实例(三) -- 爬取豆瓣首页图片

序 前面已经完成了简单网页以及伪装浏览器的学习.下面,实现对豆瓣首页所有图片爬取程序,把图片保存到本地一个路径下. 首先,豆瓣首页部分图片展示 这只是截取的一部分.下面给出,整个爬虫程序. 爬虫程序 这个爬取图片的程序采用伪装浏览器的方式,只不过是加了处理图片的模块. ''' 批量下载豆瓣首页的图片 采用伪装浏览器的方式爬取豆瓣网站首页的图片,保存到指定路径文件夹下 ''' #导入所需的库 import urllib.request,socket,re,sys,os #定义文件保存路径 targ

Web Spider实战1——简单的爬虫实战(爬取&quot;豆瓣读书评分9分以上榜单&quot;)

1.Web Spider简介 Web Spider,又称为网络爬虫,是一种自动抓取互联网网页信息的机器人.它们被广泛用于互联网搜索引擎或其他类似网站,以获取或更新这些网站的内容和检索方式.它们可以自动采集所有其能够访问到的页面内容,以供搜索引擎做进一步处理(分检整理下载的页面),而使得用户能更快的检索到他们需要的信息. 2.一个简单的网络爬虫案例 作者在浏览网页的时候看到豆瓣书单的网页(首页),如下所示: 因为书单共有409本书,17个页面,若是要一个个浏览完,需要较长的时间,想要保存好书单,那

Python 爬虫入门实例(爬取小米应用商店的top应用apk)

一,爬虫是什么? 爬虫就是获取网络上各种资源,数据的一种工具.具体的可以自行百度. 二,如何写简单爬虫 1,获取网页内容 可以通过 Python(3.x) 自带的 urllib,来实现网页内容的下载.实现起来很简单 import urllib.request url="http://www.baidu.com" response=urllib.request.urlopen(url) html_content=response.read() 还可以使用三方库 requests ,实现起

scrapy框架爬取豆瓣读书(1)

1.scrapy框架 Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试. Scrapy吸引人的地方在于它是一个框架,任何人都可以根据需求方便的修改.它也提供了多种类型爬虫的基类,如BaseSpider.sitemap爬虫等,最新版本又提供了web2.0爬虫的支持. 主要组件: 2.快速开始 scrapy startproject douban cd到douban根目录

Python爬虫之利用BeautifulSoup爬取豆瓣小说(三)——将小说信息写入文件

1 #-*-coding:utf-8-*- 2 import urllib2 3 from bs4 import BeautifulSoup 4 5 class dbxs: 6 7 def __init__(self): 8 self.pageIndex = 0 9 self.enable = True 10 self.file = None 11 self.content = [] 12 13 14 #获取html页面的内容 15 def getPage(self, pageIndex): 1

爬取豆瓣电影Top250和和豆瓣图书

豆瓣电影top250url:https://movie.douban.com/top250?start=0&filter= 首先f12进入检查CTRL +shift +c定位到标题 可以看到电影标题在<a>标签下,所以我们只要定位到a标签下就可以了,我们找到<a>标签的最上一级标签<div>标签属性class=item,好了现在我们大概有了思路了,现在让我来写代码吧 第一步:设置请求头headers headers = {'User-Agent': 'Mozil

Python爬虫入门 | 爬取豆瓣电影信息

这是一个适用于小白的Python爬虫免费教学课程,只有7节,让零基础的你初步了解爬虫,跟着课程内容能自己爬取资源.看着文章,打开电脑动手实践,平均45分钟就能学完一节,如果你愿意,今天内你就可以迈入爬虫的大门啦~好啦,正式开始我们的第二节课<爬取豆瓣电影信息>吧!啦啦哩啦啦,都看黑板~1. 爬虫原理1.1 爬虫基本原理听了那么多的爬虫,到底什么是爬虫?爬虫又是如何工作的呢?我们先从"爬虫原理"说起.爬虫又称为网页蜘蛛,是一种程序或脚本.但重点在于:它能够按照一定的规则,自动