python3 抓取图片

 2023-09-09 阅读 27 评论 0

摘要:import reimport urllib.request# import urllibimport osdef getHtml(url): page = urllib.request.urlopen(url) html = page.read() return html.decode('UTF-8')def getImg(html): reg = r'src="(.+?\.jpg)" pic_ext' # 要加
import re
import urllib.request
# import urllib
import os

def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html.decode('UTF-8')

def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext' # 要加括号,作为元组返回,抓取淘宝的图片png(先看源码中图片的地址路径)reg = r'data-lazy="(.+?\.png)" '
imgre = re.compile(reg)
imglist = imgre.findall(html)
x = 0
path = 'D:\\pythonTest\\images'
if not os.path.isdir(path):
os.makedirs(path)
paths = path + '\\' # 保存在test路径下
for imgurl in imglist:
urllib.request.urlretrieve(imgurl, '{}{}.jpg'.format(paths, x))
x = x + 1

html = getHtml("http://tieba.baidu.com/p/2460150866") # 淘宝的:html = getHtml(r"http://www.taobao.com/")
getImg(html)

转载于:https://www.cnblogs.com/vsmart/p/7768944.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/21331.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息