python抓取网页中链接的静态图片

作者:zoujm-hust12 时间:2023-04-17 04:27:27 

本文实例为大家分享了python抓取网页中链接的静态图片的具体代码,供大家参考,具体内容如下


# -*- coding:utf-8 -*-

#http://tieba.baidu.com/p/2460150866
#抓取图片地址

from bs4 import BeautifulSoup
import urllib.request
from time import sleep

html_doc = "http://tieba.baidu.com/p/2460150866"

def get_image(url):
req = urllib.request.Request(url)
webpage = urllib.request.urlopen(req)

html = webpage.read()
soup = BeautifulSoup(html, 'html.parser')

#抓取图片地址
#抓取img标签且class为BDE_Image的所有内容
img_src=soup.findAll("img",{'class':'BDE_Image'})
i = 1
for img in img_src:
 img_url = img.get('src') #抓取src
# print(img)
 req = urllib.request.Request(img_url)
 u = urllib.request.urlopen(req)
 data = u.read()
 with open("AutoCodePng20180119-"+str(i)+".jpg", 'wb') as f:
  sleep(2)
  f.write(data)
  i += 1

def getImg(url):
html = urllib.request(url)
page = html.read()
soup = BeautifulSoup(page, "html.parser")
imglist = soup.find_all('img') #发现html中带img标签的数据,输出格式为<img xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,存入集合
lenth = len(imglist) #计算集合的个数
for i in range(lenth):
 print imglist[i].attrs['src'] #抓取img中属性为src的信息,例如<img src="123456" xxxxxxxxxxxxxxxx,则输出为123456

来源:http://blog.csdn.net/shentong1/article/details/79108279

标签:python,抓取,图片
0
投稿

猜你喜欢

  • python requests使用socks5的例子

    2023-09-14 07:06:45
  • el-table嵌套el-popover处理卡顿的解决

    2024-05-09 15:22:34
  • Python+django实现简单的文件上传

    2021-08-15 03:11:25
  • mysql中explain用法详解

    2024-01-13 16:28:25
  • Mootools 1.2教程(18)——Class 类(第一部分)

    2008-12-19 12:45:00
  • 详解MySQL 数据库优化方法

    2010-08-12 14:50:00
  • 使用mss2sql工具将SqlServer转换为Mysql全记录

    2024-01-24 11:59:37
  • python变量赋值机制踩坑记录

    2021-08-19 08:59:44
  • Django在Model保存前记录日志实例

    2023-10-05 03:01:29
  • Js setInterval与setTimeout(定时执行与循环执行)的代码(可以传入参数)

    2024-04-16 09:23:29
  • Python使用sqlite3模块内置数据库

    2024-01-26 21:51:39
  • 利用Python的folium包绘制城市道路图的实现示例

    2021-01-26 02:02:16
  • PHP中大于2038年时间戳的问题处理方案

    2023-07-03 14:33:26
  • MYSQL数据库实用学习资料之常用命令集合

    2009-03-06 18:12:00
  • Python之ReportLab绘制条形码和二维码的实例

    2023-06-26 09:43:30
  • python GUI编程实现扫雷游戏

    2023-10-27 20:41:29
  • python实现五子棋游戏

    2021-09-24 13:52:37
  • 浅谈python多线程和多线程变量共享问题介绍

    2022-08-29 04:34:18
  • python中关于对super()函数疑问解惑

    2022-08-10 04:11:48
  • 解析arp病毒背后利用的Javascript技术

    2007-08-08 09:55:00
  • asp之家 网络编程 m.aspxhome.com