Python3 - 抓取静态页面(图片)
来源:互联网 发布:淘宝女装店铺公告图片 编辑:程序博客网 时间:2024/05/21 08:03
python 3.4
#!/usr/bin/env python# coding=utf-8import urllibimport urllib.requestimport reimport timefrom threading import *from bs4 import BeautifulSoup# 控制共享资源的访问数量screenLock = Semaphore(value = 1)# headersheaders = { 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36'}main_url = 'http://www.symmz.com'num = 1# set 无序不重复元素集pages = set()pages.add(main_url)# 防止jpg 重复srces = set()def downloadimg(url,depth): if depth != 0: print (depth) print (url) req = urllib.request.Request(url, headers=headers) try: res_html = urllib.request.urlopen(req).read().decode('utf-8') except urllib.HTTPError: print ('异常~') # 创建 BeautifulSoup对象 soup = BeautifulSoup(res_html,'html.parser') imgurllist = soup.find_all('img',{'src':re.compile(r'http://.+.jpg')}) urllist = soup.find_all('a',{'href':re.compile(r'/.+?/.+?.html')}) local_pash = './symmzImg01/' # 全局变量 global num for item in imgurllist: print (" src:" + item['src']) url = item['src'] if url not in srces: path = local_pash + str(num) + '.jpg' # 直接将远程文件下载到本地 urllib.request.urlretrieve(url,path) # 将图片url 存到set中 srces.add(url) num += 1 # 锁 screenLock.acquire() print (str(num) + '.jpg 已下载\n') screenLock.release() else: print('url 重复~'+url) # 深搜 for url in urllist: if url not in pages: global main_url pattern = re.compile(r'http://') newurl = main_url + url['href'] # 判断url 中是否有两个 http if(len(re.findall(pattern,newurl)) > 1): newurl = url['href'] print('newurl:'+newurl) downloadimg(newurl, depth-1) # 不要加入 重复url pages.add(url) time.sleep(1) else: return def start(): downloadimg(main_url,3)if __name__ == '__main__': start()
0 0
- Python3 - 抓取静态页面(图片)
- Python3抓取页面图片
- Python3抓取网页图片
- python3抓取百度图片
- BeautifulSoup的简单使用和抓取静态页面图片
- Python - 静态页面抓取(抓取‘糗事百科’段子)
- Python3 抓取网页中的图片
- Python3 抓取网页中的图片
- Python3.4.4抓取网页图片
- python3抓取糗百图片
- python3抓取糗百图片
- python3 抓取网页自有图片
- Python3 爬虫--批量抓取图片
- html静态页面标签抓取
- 抓取静态页面的信息
- Python3抓取中文页面显示问题
- 简单的python3 urllib3 多线程 抓取图片
- Python3 网络爬虫之抓取图片
- android 点击数字跳转到电话界面
- JAVA开发4--测试使用批处理命令调用jar包
- C#生成dll并引用161018
- 14.4.2 Change Buffer 延迟写
- CodeForces 732A Buy a Shovel(买铁铲不找零,掉分继续)
- Python3 - 抓取静态页面(图片)
- 置换表
- 常见判断
- PHP curl总结
- c# 控制摄像头
- Linux下tar命令解析
- FreeRTOS系列第4篇---FreeRTOS编码标准及风格指南
- Zobrist键值
- java高级应用之框架篇