使用Python3编写抓取网页和只抓网页图片的脚本

作者:damotiansheng 时间:2023-08-15 08:00:17 

最基本的抓取网页内容的代码实现:


#!/usr/bin/env python

from urllib import urlretrieve

def firstNonBlank(lines):
 for eachLine in lines:
   if not eachLine.strip():
     continue
   else:
     return eachLine

def firstLast(webpage):
 f = open(webpage)
 lines = f.readlines()
 f.close()
 print firstNonBlank(lines),
 lines.reverse()
 print firstNonBlank(lines),

def download(url='http://www',process=firstLast):
 try:
   retval = urlretrieve(url)[0]
 except IOError:
   retval = None
 if retval:
   process(retval)

if __name__ == '__main__':
 download()

利用urllib模块,来实现一个网页中针对图片的抓取功能:


import urllib.request
import socket
import re
import sys
import os
targetDir = r"C:\Users\elqstux\Desktop\pic"
def destFile(path):
 if not os.path.isdir(targetDir):
   os.mkdir(targetDir)
 pos = path.rindex('/')
 t = os.path.join(targetDir, path[pos+1:])
 return t

if __name__ == "__main__":
 hostname = "http://www.douban.com"
 req = urllib.request.Request(hostname)
 webpage = urllib.request.urlopen(req)
 contentBytes = webpage.read()
 for link, t in set(re.findall(r'(http:[^\s]*?(jpg|png|gif))', str(contentBytes))):
   print(link)
   urllib.request.urlretrieve(link, destFile(link))

       


import urllib.request
import socket
import re
import sys
import os
targetDir = r"H:\pic"
def destFile(path):
 if not os.path.isdir(targetDir):
   os.mkdir(targetDir)
 pos = path.rindex('/')
 t = os.path.join(targetDir, path[pos+1:]) #会以/作为分隔
 return t

if __name__ == "__main__":
 hostname = "http://www.douban.com/"
 req = urllib.request.Request(hostname)
 webpage = urllib.request.urlopen(req)
 contentBytes = webpage.read()
 match = re.findall(r'(http:[^\s]*?(jpg|png|gif))', str(contentBytes) )#r'(http:[^\s]*?(jpg|png|gif))'中包含两层圆括号,故有两个分组,
                            #上面会返回列表,括号中匹配的内容才会出现在列表中
 for picname, picType in match:
   print(picname)
   print(picType)

'''''
输出:
http://img3.douban.com/pics/blank.gif
gif
http://img3.douban.com/icon/g111328-1.jpg
jpg
http://img3.douban.com/pics/blank.gif
gif
http://img3.douban.com/icon/g197523-19.jpg
jpg
http://img3.douban.com/pics/blank.gif
gif
...
'''

标签:Python,抓取
0
投稿

猜你喜欢

  • Python动态演示旋转矩阵的作用详解

    2022-08-21 02:35:15
  • 用Dreamweaver MX制作导航下拉菜单

    2009-05-29 18:37:00
  • Python读取视频的两种方法(imageio和cv2)

    2023-10-04 13:45:06
  • python中isdigit() isalpha()用于判断字符串的类型问题

    2023-05-02 18:52:48
  • 提升网站可用性的3个忠告

    2008-01-31 13:48:00
  • Python单个项目列表转换为整数的实现

    2023-07-31 20:56:21
  • python异步Web框架sanic的实现

    2021-01-17 01:37:57
  • 使用Python实现将多表分批次从数据库导出到Excel

    2024-01-13 06:17:27
  • 浅析JavaScript中的array数组类型系统

    2024-04-17 10:04:21
  • Python正则表达式中的量词符号与组问题小结

    2022-12-14 15:24:24
  • python SVM 线性分类模型的实现

    2021-04-04 03:51:57
  • Base64编码的深入认识与理解

    2023-03-05 08:44:01
  • python 随机数使用方法,推导以及字符串,双色球小程序实例

    2023-10-11 08:48:31
  • Python 程序报错崩溃后如何倒回到崩溃的位置(推荐)

    2021-01-08 16:14:34
  • ASP编程如何执行存储过程?

    2010-03-17 20:56:00
  • Python QTimer实现多线程及QSS应用过程解析

    2023-10-26 12:34:46
  • python频繁写入文件时提速的方法

    2023-11-11 01:48:40
  • 初步解析Python中的yield函数的用法

    2023-01-03 12:23:05
  • ES6的循环与可迭代对象示例详解

    2024-05-02 17:25:22
  • 巧制可全屏拖动的图片

    2008-05-09 19:34:00
  • asp之家 网络编程 m.aspxhome.com