• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 使用Python3编写抓取网页和只抓网页图片的脚本

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python3抓取网页,python3抓取网页数据,python3爬虫抓取图片,python3抓取图片,python3网页爬虫等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

#!/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() 
</div>

利用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)) 
</div>

       

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 
... 
''' 

</div>

</div>

您可能想查找下面的文章:

  • python3实现抓取网页资源的 N 种方法
  • python3实现抓取网页资源的 N 种方法
  • 使用Python3编写抓取网页和只抓网页图片的脚本
  • Python实现抓取城市的PM2.5浓度和排名

相关文章

  • 用python + openpyxl处理excel2007文档思路以及心得
  • wxPython窗口中文乱码解决方法
  • python中快速进行多个字符替换的方法小结
  • Python import自定义模块方法
  • Python中的数据对象持久化存储模块pickle的使用示例
  • 详细讲解Python中的文件I/O操作
  • Python抓取京东图书评论数据
  • python统计字符串中指定字符出现次数的方法
  • 浅谈tensorflow中Dataset图片的批量读取及维度的操作详解
  • 学习python之编写简单简单连接数据库并执行查询操作

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python translator使用实例
    • python模拟enum枚举类型的方法小结
    • 详解python 字符串和日期之间转换 StringAndDate
    • 跟老齐学Python之集成开发环境(IDE)
    • 使用Python进行新浪微博的mid和url互相转换实例(10进制和62进制互算)
    • Python中分数的相关使用教程
    • 为python设置socket代理的方法
    • 浅谈Python 集合(set)类型的操作——并交差
    • 用Python编写一个基于终端的实现翻译的脚本
    • 介绍Python的Django框架中的QuerySets

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有