python+BeautifulSoup+多进程爬取糗事百科图片
用到的库;
import requests
import os
from bs4 import BeautifulSoup
import time
from multiprocessing import Pool
定义图片存储路径;
path = r'E:\爬虫\0805\\'
请求头,模拟浏览器请求;
在浏览器中的位置,按f12打开开发者模式;

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
主函数;
def get_images(url):
data = 'https:'
res = requests.get(url,headers=headers)
soup = BeautifulSoup(res.text,'lxml')
url_infos = soup.select('div.thumb > a > img')
# print(url_infos)
for url_info in url_infos:
try:
urls = data+url_info.get('src')
if os.path.exists(path+urls.split('/')[-1]):
print('图片已下载')
else:
image = requests.get(urls,headers=headers)
with open(path+urls.split('/')[-1],'wb') as fp:
fp.write(image.content)
print('正在下载:'+urls)
time.sleep(0.5)
except Exception as e:
print(e)
开始爬虫程序;
if __name__ == '__main__':
# 路由列表
urls = ['https://www.qiushibaike.com/imgrank/page/{}/'.format(i) for i in range(1,14)]
# 开启多进程爬取
pool = Pool()
pool.map(get_images,urls)
print('抓取完毕')
爬取中;

打开文件夹查看爬取结果;

done
完整代码;
import requests
import os
from bs4 import BeautifulSoup
import time
from multiprocessing import Pool
"""
************常用爬虫库***********
requests
BeautifulSoup
pyquery
lxml
************爬虫框架***********
scrapy
三大解析方式:re,css,xpath
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
path = r'E:\爬虫\0805\\'
def get_images(url):
data = 'https:'
res = requests.get(url,headers=headers)
soup = BeautifulSoup(res.text,'lxml')
url_infos = soup.select('div.thumb > a > img')
# print(url_infos)
for url_info in url_infos:
try:
urls = data+url_info.get('src')
if os.path.exists(path+urls.split('/')[-1]):
print('图片已下载')
else:
image = requests.get(urls,headers=headers)
with open(path+urls.split('/')[-1],'wb') as fp:
fp.write(image.content)
print('正在下载:'+urls)
time.sleep(0.5)
except Exception as e:
print(e) if __name__ == '__main__':
# 路由列表
urls = ['https://www.qiushibaike.com/imgrank/page/{}/'.format(i) for i in range(1,14)]
# 开启多进程爬取
pool = Pool()
pool.map(get_images,urls)
print('抓取完毕')
python+BeautifulSoup+多进程爬取糗事百科图片的更多相关文章
- python+正则+多进程爬取糗事百科图片
话不多说,直接上代码: # 需要的库 import requests import re import os from multiprocessing import Pool # 请求头 header ...
- python爬虫之爬取糗事百科并将爬取内容保存至Excel中
本篇博文为使用python爬虫爬取糗事百科content并将爬取内容存入excel中保存·. 实验环境:Windows10 代码编辑工具:pycharm 使用selenium(自动化测试工具)+p ...
- Python爬虫:爬取糗事百科
网上看到的教程,但是是用正则表达式写的,并不能运行,后面我就用xpath改了,然后重新写了逻辑,并且使用了双线程,也算是原创了吧#!/usr/bin/python# -*- encoding:utf- ...
- 8.Python爬虫实战一之爬取糗事百科段子
大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...
- python网络爬虫--简单爬取糗事百科
刚开始学习python爬虫,写了一个简单python程序爬取糗事百科. 具体步骤是这样的:首先查看糗事百科的url:http://www.qiushibaike.com/8hr/page/2/?s=4 ...
- Python爬虫实战一之爬取糗事百科段子
大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...
- 转 Python爬虫实战一之爬取糗事百科段子
静觅 » Python爬虫实战一之爬取糗事百科段子 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致 ...
- [爬虫]用python的requests模块爬取糗事百科段子
虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “HTTP for Humans”,说明使用更 ...
- 初识python 之 爬虫:使用正则表达式爬取“糗事百科 - 文字版”网页数据
初识python 之 爬虫:使用正则表达式爬取"古诗文"网页数据 的兄弟篇. 详细代码如下: #!/user/bin env python # author:Simple-Sir ...
随机推荐
- [ARM-Linux开发] 主设备号--驱动模块与设备节点联系的纽带
一.如何对设备操作 linux中对设备进行操作是通过文件的方式进行的,包括open.read.write.对于设备文件,一般称其为设备节点,节点有一个属性是设备号(主设备号.次设备号),其中主设备号将 ...
- Slenium入门
selenium 为浏览器测试框架,可以调用浏览器webdriver模拟浏览器操作360打开Chrome: from selenium import webdriver from selenium.w ...
- vue脚手架(vue-cli)老版本(2.xx版)的使用
1. 在idea新建一个Static Web项目 2. 在新建的项目下打开cmd 3. 输入安装脚手架命令:npm install -g vue-cli 4. 查看安装的版本:vue -V (2. ...
- opencv之重映射
好久没写呆码了 今天发个重映射 #include "opencv2/video/tracking.hpp" #include "opencv2/imgproc/imgpr ...
- composer安装FOSUserBundle内存溢出
内存溢出异常: Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 4096 bytes ...
- Python 入门(1):hello world 到流程控制
1.hello world 在D:\python\目录下新建文件hello.txt,编写代码如下 print("hello world!") 修改后缀名为.py,执行hello.p ...
- C语言 小球撞击反弹
计算法(略) #include <stdio.h> #include <stdlib.h> int main() { int x, y, a, resu; scanf(&quo ...
- C语言的变参列表 va_list
1. va_list的基本原理和用法 #include<stdio.h> #include<stdarg.h> void func(int i,char *ch,...){ / ...
- Go基础编程实践(一)—— 操作字符串
修剪空格 strings包中的TrimSpace函数用于去掉字符串首尾的空格. package main import ( "fmt" "strings" ) ...
- 备份和还原 第三篇:master 数据库的备份和还原
在SQL Server 中,master 数据库记录系统级别的元数据,例如,logon accounts, endpoints, linked servers, and system configur ...