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

Python使用urllib2模块抓取HTML页面资源的实例分享

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

通过本文主要向大家介绍了python urllib2模块,python3 urllib2,python urllib2,python3.5 urllib2,python中urllib2等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

先把要抓取的网络地址列在单独的list文件中

http://www.jb51.net/article/83440.html
http://www.jb51.net/article/83437.html
http://www.jb51.net/article/83430.html
http://www.jb51.net/article/83449.html
</div>

然后我们来看程序操作,代码如下:

#!/usr/bin/python

import os
import sys
import urllib2
import re

def Cdown_data(fileurl, fpath, dpath):
 if not os.path.exists(dpath):
  os.makedirs(dpath)
 try:
  getfile = urllib2.urlopen(fileurl) 
  data = getfile.read()
  f = open(fpath, 'w')
  f.write(data)
  f.close()
 except:
 print 

with open('u1.list') as lines:
 for line in lines:
  URI = line.strip()
  if '?' and '%' in URI:
   continue
 elif URI.count('/') == 2:
   continue
  elif URI.count('/') > 2:
   #print URI,URI.count('/')
  try:
    dirpath = URI.rpartition('/')[0].split('//')[1]
    #filepath = URI.split('//')[1].split('/')[1]
    filepath = URI.split('//')[1]
   if filepath:
     print URI,filepath,dirpath
     Cdown_data(URI, filepath, dirpath)
   except:
    print URI,'error'
</div>

</div>

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

  • 【Python】Python的urllib模块、urllib2模块批量进行网页下载文件
  • Python网络编程中urllib2模块的用法总结
  • Python使用urllib2模块抓取HTML页面资源的实例分享
  • Python使用urllib2模块抓取HTML页面资源的实例分享
  • 使用Python的urllib2模块处理url和图片的技巧两则
  • 使用Python的urllib2模块处理url和图片的技巧两则
  • Python中的urllib模块使用详解
  • python基于urllib实现按照百度音乐分类下载mp3的方法
  • Python urllib、urllib2、httplib抓取网页代码实例
  • 介绍Python的Urllib库的一些高级用法

相关文章

  • Python使用面向对象方式创建线程实现12306售票系统
  • Python实现各种排序算法的代码示例总结
  • Python中的集合类型知识讲解
  • Python笔记(叁)继续学习
  • Python深入学习之装饰器
  • Python open读写文件实现脚本
  • 深入理解Python中字典的键的使用
  • Python常用的内置序列结构(列表、元组、字典)学习笔记
  • Python实现以时间换空间的缓存替换算法
  • python使用PyV8执行javascript代码示例分享

文章分类

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

最近更新的内容

    • Python的Flask框架中的Jinja2模板引擎学习教程
    • 举例讲解Python中装饰器的用法
    • Python删除空文件和空文件夹的方法
    • Python3.x中自定义比较函数
    • python爬虫入门教程之点点美女图片爬虫代码分享
    • Python类属性与实例属性用法分析
    • Python中类的定义、继承及使用对象实例详解
    • Python中的异常处理相关语句基础学习笔记
    • python通过邮件服务器端口发送邮件的方法
    • 在Python中使用模块的教程

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

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