1. import chardet
  2. import urllib.request
  3.  
  4. page = urllib.request.urlopen('http://photo.sina.com.cn/') #打开网页
  5. htmlCode = page.read() #获取网页源代码
  6.  
  7. print(chardet.detect(htmlCode)) #打印返回网页的编码方式
  1. {'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}
  1. data = htmlCode.decode('utf-8')
  2. print(data) #打印网页源代码
  1. pageFile = open('E:\\pageCode.txt','wb')#以写的方式打开pageCode.txt
  2. pageFile.write(htmlCode)#写入
  3. pageFile.close()#开了记得关
  1. 获取其他信息
  2.  
  3. 打开pageCode.txt文件(也可以直接在原网页F12调试获取),查看需要获取数据的标签信息。
  4.  
  5. 比如我现在要拿图片
  6.  
  7. 写出图片的正则表达式: reg = r'src="(.+?\.jpg)"'
  8.  
  9. 解释下吧——匹配以src="开头然后接一个或多个任意字符(非贪婪),以.jpg" 结尾的字符串。比如图中红框内src 双引号里的链接就是一个匹配的字符串。
  10.  
  11. 接着我们要做的就是从get_html方法返回的辣么长一串字符串中 拿到 满足正则表达式的 字符串。
  12.  
  13. 用到python中的re库中的 re.findall(str) 它返回一个满足匹配的字符串组成的列表
  1. import re
  2. import chardet
  3. import urllib.request
  4.  
  5. page = urllib.request.urlopen('http://www.meituba.com/tag/juesemeinv.html') #打开网页
  6. htmlCode = page.read() #获取网页源代码
  7.  
  8. #print(chardet.detect(htmlCode)) #查看编码方式
  9. data = htmlCode.decode('utf-8')
  10. #print(data) #打印网页源代码
  11.  
  12. #pageFile = open('pageCode.txt','wb')#以写的方式打开pageCode.txt
  13. #pageFile.write(htmlCode)#写入
  14. #pageFile.close()#开了记得关
  15.  
  16. reg = r'src="(.+?\.jpg)"'#正则表达式
  17. reg_img = re.compile(reg)#编译一下,运行更快
  18. imglist = reg_img.findall(data)#进行匹配
  19. for img in imglist:
  20. print(img)
  1. http://ppic.meituba.com:83/uploads3/181201/3-1Q20111553V11.jpg
  2. http://ppic.meituba.com:83/uploads2/180622/3-1P62215532D61.jpg
  3. http://ppic.meituba.com:83/uploads2/180605/3-1P6051000144I.jpg
  4. http://ppic.meituba.com:83/uploads2/170511/8-1F5110URc35.jpg
  5. http://ppic.meituba.com:83/uploads/160322/8-1603220U50O23.jpg
  6. http://ppic.meituba.com:83/uploads2/180317/3-1P31F91U1X9.jpg
  7. http://ppic.meituba.com:83/uploads/160718/7-160GQ51G0b4.jpg
  8. http://ppic.meituba.com:83/uploads2/170517/8-1F51G50301Q3.jpg
  9. http://ppic.meituba.com:83/uploads/161010/7-1610101A202B0.jpg
  10. http://ppic.meituba.com:83/uploads2/171102/7-1G102093511F7.jpg
  11. http://ppic.meituba.com:83/uploads2/170901/7-1FZ1100545438.jpg
  12. http://ppic.meituba.com:83/uploads/160625/8-160625093044631.jpg
  13. http://ppic.meituba.com:83/uploads/160419/7-160419161553153.jpg
  14. http://ppic.meituba.com:83/uploads2/170323/7-1F323103404A2.jpg
  15. http://ppic.meituba.com:83/uploads2/170322/7-1F322105R1255.jpg
  16. http://ppic.meituba.com:83/uploads2/170211/7-1F21110040Y63.jpg
  17. http://ppic.meituba.com:83/uploads2/170110/7-1F110102005930.jpg
  18. http://ppic.meituba.com:83/uploads/160618/8-16061Q04450391.jpg
  19. http://ppic.meituba.com:83/uploads2/170330/3-1F3301HI6138.jpg
  20. http://ppic.meituba.com:83/uploads2/161230/4-161230100U5V8.jpg
  1. 然后将图片下载到本地
  2.  
  3. urllib库中有一个 urllib.request.urlretrieve(链接,名字) 方法,它的作用是以第二个参数为名字下载链接中的内容,我们来试用一下
  1. x = 0
  2. for img in imglist:
  3. print(img)
  4. urllib.request.urlretrieve('http://ppic.meituba.com/uploads/160322/8-1603220U50O23.jpg', '%s.jpg' % x)
  5. x += 1
  1. import re
  2. import urllib.request
  3.  
  4. def getGtmlCode():
  5. html = urllib.request.urlopen("http://www.quanshuwang.com/book/44/44683").read() #获取网页源代码
  6. html = html.decode("gbk") #转成该网站格式
  7. reg = r'<li><a href="(.*?)" title=".*?">(.*?)</a></li>' #根据网站样式匹配的正则:(.*?)可以匹配所有东西,加括号为我们需要的
  8. reg = re.compile(reg)
  9. urls = re.findall(reg, html)
  10. for url in urls:
  11. #print(url)
  12. chapter_url = url[0] #章节路径
  13. chapter_title = url[1] #章节名
  14. chapter_html = urllib.request.urlopen(chapter_url).read() #获取该章节的全文代码
  15. chapter_html = chapter_html.decode("gbk")
  16. chapter_reg = r'</script>&nbsp;&nbsp;&nbsp;&nbsp;.*?<br />(.*?)<script type="text/javascript">' #匹配文章内容
  17. chapter_reg = re.compile(chapter_reg,re.S)
  18. chapter_content = re.findall(chapter_reg, chapter_html)
  19. for content in chapter_content:
  20. content = content.replace("&nbsp;&nbsp;&nbsp;&nbsp;","") #使用空格代替
  21. content = content.replace("<br />","") #使用空格代替
  22. print(content)
  23. f = open('E:\\aa\\{}.txt'.format(chapter_title),'w') #保存到本地
  24. f.write(content)
  25.  
  26. getGtmlCode()

吴裕雄--python学习笔记:爬虫的更多相关文章

  1. 吴裕雄--python学习笔记:爬虫基础

    一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...

  2. 吴裕雄--python学习笔记:爬虫包的更换

    python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...

  3. 吴裕雄--python学习笔记:sqlite3 模块

    1 sqlite3.connect(database [,timeout ,other optional arguments]) 该 API 打开一个到 SQLite 数据库文件 database 的 ...

  4. 吴裕雄--python学习笔记:os模块函数

    os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.getcwd:得 ...

  5. 吴裕雄--python学习笔记:os模块的使用

    在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块. 1.当前路径及路径下 ...

  6. 吴裕雄--python学习笔记:BeautifulSoup模块

    import re import requests from bs4 import BeautifulSoup req_obj = requests.get('https://www.baidu.co ...

  7. 吴裕雄--python学习笔记:通过sqlite3 进行文字界面学生管理

    import sqlite3 conn = sqlite3.connect('E:\\student.db') print("Opened database successfully&quo ...

  8. 吴裕雄--python学习笔记:sqlite3 模块的使用与学生信息管理系统

    import sqlite3 cx = sqlite3.connect('E:\\student3.db') cx.execute( '''CREATE TABLE StudentTable( ID ...

  9. python学习笔记——爬虫学习中的重要库urllib

    1 urllib概述 1.1 urllib库中的模块类型 urllib是python内置的http请求库 其提供了如下功能: (1)error 异常处理模块 (2)parse url解析模块 (3)r ...

随机推荐

  1. 67)vector的begin() end() 和 front() back()的区别 rbegin() rend()

    1) ·············· 2)`````````v1.begin() 和v1.end()  是作为迭代器v1的 第一个位置  和 最后一个元素的下一个位置. `````````````v1. ...

  2. Ubuntu 安装软件时显示:无法获得锁 /var/lib/dpkg/lock -open(资源暂时不可用)

    出错状况:在用 sudo apt-get install 安装软件时,结果终端提示: 无法获得锁 /var/lib/dpkg/lock -open(资源暂时不可用) 无法锁定管理目录(var/lib/ ...

  3. 题解 P3117 【[USACO15JAN]牛的矩形Cow Rectangles】

    暴力什么的就算了,贪心他不香吗 这题其实如果分开想,就三种情况需要讨论:(由于不会发图,只能手打) 1) 5 . . . . . 4 . . . . . 3 . . . H . 2 . . G . . ...

  4. memset为int型数组初始化问题

    头文件:#include <string.h>memset() 函数用来将指定内存的前n个字节设置为特定的值,其原型为:    void * memset( void * ptr, int ...

  5. coursera课程视频

    #!/usr/bin/env python # coding=utf-8 import urllib import urllib2 import cookielib def setcookie(ena ...

  6. E - Ingredients 拓扑排序+01背包

    题源:https://codeforces.com/gym/101635/attachments 题意: n行,每行给定字符串s1,s2,s3代表一些菜谱名.s2和s3是煮成是的必要条件,然后给出c和 ...

  7. [原]CreateFile中的dwShareMode

    原 总结 API  一直对CreateFile的参数dwDesiredAccess和dwShareMode有什么不同不是很清楚,今天重读 windows核心编程的时候终于豁然开朗了. 真是书读百遍,其 ...

  8. springmvc 实现文件上传

    1.添加jar包 <dependency> <groupId>commons-io</groupId> <artifactId>commons-io&l ...

  9. AtCoder Beginner Contest 126

    因为本人rating太低,这场比赛还要记rating就来划水了,果然垫底了. 6题都很水,于是头一次在网赛中AK,不过由于网卡+手速太慢还是成功垫底. ABE 签到.不贴代码了,因为A考察字符串读入和 ...

  10. 2017NOIP模拟赛三 A酱的体育课

    据说改编自$CodeM 美团点评编程大赛初赛A 轮$ 简单的水题...考试的时候没想到,xjb打了暴力. 显然,第$x$个人排在第$y$个位置的情况总数为$(n-1)!$,在这些情况中,第$x$人对答 ...