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 ...
随机推荐
- Tensorflow不同版本要求与CUDA及CUDNN版本对应关系
参考官网地址: Windows端:https://tensorflow.google.cn/install/source_windows CPUVersion Python version Compi ...
- java 快速定位线上cpu偏高
1.top -c 加 大写P 查找高进程ID 2.top -Hp 加 大写 P 查找高线程ID 3.printf '%x\n' 线程ID 转成16进制 4.jstack 进程ID | grep 16进 ...
- 安装 mysql-5.6.41-winx64
REF:https://www.cnblogs.com/cwb292/p/9888039.html https://dev.mysql.com/get/Downloads/MySQLInstaller ...
- docker swarm集群搭建及使用Portainer、shipyard
一.规划 1.swarm01作为manager节点,swarm02和swarm03作为worker节点. # cat /etc/hosts 127.0.0.1 localhost 192.168. ...
- [ERROR ]Failed to execute goal org.codehaus.mojo:flatten-maven-plugin:1.1.0:flatten (flatten) on project
今天在启动项目的时候,莫名的Maven install命令的时候出现错误 错误提示:Failed to execute goal org.codehaus.mojo:flatten-maven-plu ...
- Tomcat 的端口被占用的解决办法
在dos下,输入 netstat -ano|findstr 8080 //说明:查看占用8080端口的进程 显示占用端口的进程 taskkill /pid 6856 /f //说明,运行 ...
- python入门之与用户交互
目录 一.程序与用户交互 1.1 什么是与用户交互 1.2 为什么要与用户交互 1.3 如何与用户交互 1.4 python2和python3中input的区别 1.4.1 python3中的inpu ...
- 分页Model
<?php namespace Admin\Model; use Think\Model; class PageModel extends Model{ /* *此函数分页函数.传入表名和当前页 ...
- Delphi RSA加解密【 (RSA公钥加密,私钥解密)、(RSA私钥加密,公钥解密)、MD5加密、SHA加密】
作者QQ:(648437169) 点击下载➨delphi RSA加解密 [Delphi RSA加解密]支持 (RSA公钥加密,私钥解密).(RSA私钥加密,公钥解密).MD5加密.SHA1加密.SHA ...
- golang(一)
开篇先来个Go语言的吉祥物-金花鼠Gordon. golang是谷歌2009年发布的开源编程语言,截止目前go的release版本已经到了1.10.go语言的开发人员都是计算机界大神一般的存在: Th ...