前言

emmmm

python简单图片抓取

 1 import requests
2 import threading
3 import queue
4 from subprocess import Popen,PIPE
5 from bs4 import BeautifulSoup as bs
6 import urllib
7 import base64
8
9
10 queue=queue.Queue()
11
12 class Jiandan(threading.Thread):
13 def __init__(self,queue):
14 threading.Thread.__init__(self)
15 self._queue=queue
16
17 def run(self):
18 while not self._queue.empty():
19 url=self._queue.get_nowait()
20 self.spider(url)
21
22 def spider(self,url):
23 headers={}
24 r=requests.get(url)
25 #选择lxml解析器
26 soup=bs(r.content,'lxml')
27 #查找html标签name为img的元素获取到它
28 imgs=soup.find_all(name='img',attrs={})
29
30 for img in imgs:
31 if 'onload' in str(img):
32 img=img['org_src']
33 else:
34 img=img['src']
35
36 name=img.split('/')[-1]
37
38 #获取的url没http:所以要加上http协议才能访问下载
39 img="http:"+img
40
41 #存储图片的路径地址
42 lu="C:\\Users\\xhds1\\Desktop\\img\\"+name
43 print(lu)
44 urlretrieve(img,lu) #urlretrieve模块下载图片
45 # print(name)
46
47 def main():
48 sss=""
49 for i in range(137,139):
50 riqi="20200121-%s"%(i)
51
52 #使用了base64模块进行URL编码 这里遇到的问题是编码后就成为了字节流类型
53 #查了资料才得知必须转换成字符串类型才行 纠结了好久
54 strbs=base64.b64encode(riqi.encode(encoding="utf-8")).decode("utf-8")
55
56 queue.put("https://jandan.net/pic/"+strbs+"#comments")
57
58 threads=[]
59 thread_count=5
60
61 for i in range(thread_count):
62 threads.append(Jiandan(queue))
63 for t in threads:
64 t.start()
65 for t in threads:
66 t.join()
67
68 if __name__=='__main__':
69 main()

参考学习:

浅析Python3中的bytes和str类型:https://www.cnblogs.com/chownjy/p/6625299.html

https://www.cnblogs.com/OliverQin/p/8641700.html

最新文章

  1. READ TABLE ..... BINARY SEARCH问题
  2. Wijmo 5 与Breeze 的组合,及与METRONIC 的集成
  3. ubunto安装pycharm
  4. Android自定义View自定义属性
  5. Leetcode 257 Binary Tree Paths 二叉树 DFS
  6. JavaBean简单及使用
  7. arcgis mdb和gdb编辑区别
  8. Unite Beijing 2015大型活动
  9. Oracle EBS-SQL (PO-13):检查采购物料无一揽子协议价格.sql
  10. js getByClass函数封装
  11. 10款超酷实用的jQuery焦点图赏析及源码
  12. 【转】JDBC学习笔记(3)——复习和练习
  13. PHP环境配置(1)
  14. 【C编程基础】C编译链接命令gccc
  15. spring 容器的基础 XmlBeanFactory
  16. Spring AOP的底层实现原理
  17. [转]python 装饰器
  18. 关于kingoroot这款软件
  19. Oracle 12c启动时PDBs的自动打开
  20. PHP Libevent扩展安装及应用

热门文章

  1. Eclipse JSP +Tomcat 环境搭建 错误记录
  2. 第七章 HTTP协议原理
  3. Business Partner - 供应商与客户的集成 - S/4HANA(1)
  4. 【转】Python3 正则表达式特殊符号及用法(详细列表)
  5. pyqt5屏幕坐标系
  6. 线程池CachedThreadPool
  7. window 属性:自定义元素(custom elements)
  8. JavaScript实现基于对象的队列
  9. 小白:String函数总结
  10. 基于flask的python注册到eureka