python技术分享_Python技术分享:爬虫
web数据获取
urllib模块应用
如何通过python获取网页数据
做转码
准备web页面素材
启动httpd
通过apache的访问日志 发现是python进行的登录
解决为 urllib添加头部信息
import urllib.request as urequest = u.Request("http://192.168.86.11") #将网页地址添加到request实例(变量)request.add_header("User-Agent","Mozilla/5.0 \(Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0") #为该实例添加头部信息response = u.urlopen(request) #通过urlopen打开实例(网页地址 和 头部信息)html = response.read()print(html) #访问页面
验证linux apache的日志
vim /var/log/httpd/access_log 查看信息记录是否还有python信息
下载图片的程序
import urllib.request as urequest = u.Request("http://192.168.86.11/style/\u24020836931378817798fm170s6BA8218A7B2128178FA0A49F010080E2w.jpg") #图片地址request.add_header("User-Agent","Mozilla/5.0 \(Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0")response = u.urlopen(request)html = response.read() #读取的图片的2进制数据#print(html)with open("c:\\users\\allen\\desktop\\爬虫.jpg","wb") as f:
f.write(html)
需要将网页信息获取程序转换为函数模式
import urllib.request as uurl = "http://192.168.86.11"def get_html(urladdr):
"我的功能是获取主页的所有源码"
request = u.Request(urladdr)
request.add_header("User-Agent","Mozilla/5.0 \ (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0")
response = u.urlopen(request)
html = response.read()
return htmldef get_imglist():
"我的功能是将所有图片信息地址 做成一个大的列表"
passdef get_imgs():
"我的功能是下载图片列表中的所有 图片信息 并保存图片"
passhtml = get_html(url)print(html)
如何用正则匹配字符串
单个字符匹配
"." 匹配单个任意字符
>>> import re>>> re.findall(".ood","I say Good not food")['Good', 'food']>>> re.findall(".ood","I say Good not food @ood")['Good', 'food', '@ood']>>> re.findall(".ood","I say Good not food ood")['Good', 'food', ' ood']>>> re.findall(".ood","I say Good not food \nood")['Good', 'food']>>>
[] 单个字符逐一匹配
>>> re.findall("[fn]ood","I say Good not food nood") #ood以f或者n链接 的字符串['food', 'nood']>>> re.findall("[^fn]ood","I say Good not food nood")#ood不是以f或者n链接的字符串 取反['Good']>>> re.findall("^[Gfn]ood","Good not food nood") #以G f n 开头的和ood链接的字符串匹配['Good']>>> re.findall("^[Gfn]ood","I say Good not food nood")[]>>>
\d 匹配单个0-9
>>> re.findall("\d","How old are you? I am 36")['3', '6']>>> re.findall("\d\d","How old are you? I am 36")['36']>>>
\w 匹配0-9a-zA-Z_ 该范围内的单个字符
>>> re.findall("\w","How old are you? I am 36")['H', 'o', 'w', 'o', 'l', 'd', 'a', 'r', 'e', 'y', 'o', 'u', 'I', 'a', 'm', '3', '6']>>> re.findall("\w\w\w","How old are you? I am 36")['How', 'old', 'are', 'you']>>> re.findall("\w\w","How old are you? I_am 36")['Ho', 'ol', 'ar', 'yo', 'I_', 'am', '36']>>>
\s 匹配空白字符以及空格
>>> re.findall("\s","\tHow old are you?\r\n")['\t', ' ', ' ', ' ', '\r', '\n']>>>
一组字符匹配
逐字匹配
>>> re.findall("allen","I am allen")['allen']>>> re.findall("allen","I am allenallen")['allen', 'allen']>>>
逐字匹配 | 分割不同的字符串
>>> re.findall("food|nood","I say Good not food nood")['food', 'nood']>>> re.findall("not|nood","I say Good not food nood")['not', 'nood']>>>
*表示左邻第一个字符 出现0次到无穷次
>>> re.findall("go*gle","I like google not ggle goooogle and gogle")['google', 'ggle', 'goooogle', 'gogle']>>>
+表示左邻第一个字符 出现1次到无穷次
>>> re.findall("go+gle","I like google not ggle goooogle and gogle")['google', 'goooogle', 'gogle']>>>
?表示左邻第一个字符 出现0次或1次
>>> re.findall("go?gle","I like google not ggle goooogle and gogle")['ggle', 'gogle']
{}指定左邻字符出现的次数
>>> re.findall("gogle","I like google not ggle goooogle and gogle")['google']>>> re.findall("gogle","I like google not ggle goooogle and gogle")['gogle']>>> re.findall("gogle","I like google not ggle goooogle and gogle")['google', 'goooogle', 'gogle']>>>
根据以上信息完成web测试页面图片获取
import urllib.request as uimport reurl = "http://192.168.86.11/" #结尾添加左斜杠def get_html(urladdr):
"我的功能是获取主页的所有源码"
request = u.Request(urladdr)
request.add_header("User-Agent","Mozilla/5.0 \ (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0")
response = u.urlopen(request)
html = response.read()
return htmldef get_imglist(url,html):
"我的功能是将所有图片信息地址 做成一个大的列表"
imglist = [] #存储图片地址的一个容器列表
bytsimglist = re.findall(b"style/\w\.jpg",html)
for i in bytsimglist: #因为图片地址不全而且是2进制字符串 因此 要进行拼接处理
imgaddr = url+str(i,encoding='utf8') #拼接并且转换为字符串
imglist.append(imgaddr) #将地址放入列表中
return imglist
def get_imgs(imglist):
"我的功能是下载图片列表中的所有 图片信息 并保存图片"
num = 0 #为了图片名称进行自增
for imgurl in imglist:
num += 1
data = get_html(imgurl)
with open("%s.jpg" %num,"wb") as f: #图片名字会从1.jpg开始一直到54.jpg
f.write(data)html = get_html(url)#print(html)imglist = get_imglist(url,html)#print(len(imglist))get_imgs(imglist)
布卡漫画网站 资源爬取
import urllib.request as u
import re
#url = "http://www.buka.cn/view/223172/65537.html"
#url = "http://www.buka.cn/view/223578/65537.html"
#url = "http://www.buka.cn/view/221784/65540.html"
url = "http://www.buka.cn/view/219792/65742.html"
def get_html(urladdr):
"我的功能是获取主页的所有源码"
request = u.Request(urladdr)
request.add_header("User-Agent","Mozilla/5.0 \
(Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0")
response = u.urlopen(request)
html = response.read()
return html
def get_imglist(url,html):
"我的功能是将所有图片信息地址 做成一个大的列表"
imglist = [] #存储图片地址的一个容器列表
bytsimglist = re.findall(b"http://i-cdn.ibuka.cn/pics/\d+/\d+/\w+.jpg",html)
#print(bytsimglist)
for i in bytsimglist:
imglist.append(str(i,encoding='utf8'))
return imglist
def get_imgs(imglist):
"我的功能是下载图片列表中的所有 图片信息 并保存图片"
num = 0 #为了图片名称进行自增
for imgurl in imglist:
num += 1
data = get_html(imgurl)
with open("%s.jpg" %num,"wb") as f: #图片名字会从1.jpg开始一直到54.jpg
f.write(data)
html = get_html(url)
#print(html)
imglist = get_imglist(url,html)
#print(imglist)
get_imgs(imglist)
正则匹配中特殊符号应用
^表示已什么开头 $以什么结尾
>>> re.findall('^I say',"I say Good not food")['I say']>>> re.findall('not food$',"I say Good not food")['not food']>>> re.findall('not Good$',"I say Good not food")[]>>>
\b 指定单词边界 _不属于特殊符号
>>> re.findall("allen","allen.com allen_123 allen.com")['allen', 'allen', 'allen']>>> re.findall("\ballen\b","allen.com allen_123 allen.com")[]>>> re.findall("\\ballen\\b","allen.com allen_123 allen.com")['allen', 'allen']>>>
python技术分享_Python技术分享:爬虫相关推荐
- python母亲节代码_python实践分享提高代码质量:使用with自动关闭资源
使用with自动关闭资源 来做个简单的试验,观察一下发生的现象.在Python解释器中输入下面两行代码,会有什么情况发生呢? 使用with自动关闭资源 结果是:在解释器所在的目录下生成了一个文件tes ...
- python 携程_python 携程爬虫开发笔记
前言 最近购买了<Python3 爬虫.数据清洗与可视化实战>,刚好适逢暑假,就尝试从携程页面对广州的周边游产品进行爬虫数据捕捉. 因为才学Python不够一个星期,python的命名规范 ...
- python soup歌词_python学习之爬虫(一) ——————爬取网易云歌词
接触python也有一段时间了,一提到python,可能大部分pythoner都会想到爬虫,没错,今天我们的话题就是爬虫!作为一个小学生,关于爬虫其实本人也只是略懂,怀着"Done is b ...
- python 静态网页_Python静态网页爬虫相关知识
想要开发一个简单的Python爬虫案例,并在Python3以上的环境下运行,那么需要掌握哪些知识才能完成一个简单的Python爬虫呢? 爬虫的架构实现 爬虫包括调度器,管理器,解析器,下载器和输出器. ...
- python 京东 价格_python 京东商品价格爬虫示例
这篇文章主要为大家详细介绍了python 京东商品价格爬虫示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! 闲着没事 ...
- python组件介绍_python 中的爬虫· scrapy框架 重要的组件的介绍
一 . 去重的规则组件 去重数据,中通过set() 去重的, 留下的数据存在redis 中, 找到这个类 : from scrapy.dupefilter import RFPDupeFilter ...
- 悟空问答python反爬_Python写个爬虫碰到反爬了,怎么办那就动手破坏它!
搞定javascript加密 js加密最简单的是采用md5进行的,我们通过http://fanyi.youdao.com/来演示本篇博客内容 接下来你要注意的是这个请求是由哪个Js文件发起的 文件获取 ...
- 关于在Windows环境下对AliExpress反爬虫技术突破的一些分享
Python爬虫之Aliexpress反爬虫技术的分享 关于在Windows环境下对AliExpress反爬虫技术突破的一些分享 在公司工作的时候公司的业务涉及到了对应爬虫任务,然后根据平时积累的知识 ...
- Python常用6个技术网站汇总分享!
Python是一门面向对象的编程语言,它具有丰富和强大的库,能够把用其他语言编写的各种模块轻松地联结在一起,因此也常被称为"胶水语言".Python技术会随着互联网的不断发展一直迭 ...
最新文章
- python空值填充_pandas | DataFrame基础运算以及空值填充
- 搜狗输入法漏洞获取系统权限0day再述
- Excel超级链接方式应用技巧
- Objective - C基础: 第一天 - 5.对象和类
- C# 操作XML入门
- python快捷_汇总学习Python必备的42个快捷键,看完收获满满
- 文章目录列表的字体颜色修改
- ssl1125-集合【哈希表二分查找+快排】
- Java面试知识点:Date类、异常
- linux标准I/O——按字符输入和输出
- loss下降auc下降_梯度下降算法 线性回归拟合(附Python/Matlab/Julia源代码)
- final 最终 java 1614876717
- tomcat 查看当前请求数_原生线程池这么强大,Tomcat 为何还需扩展线程池?
- pip下载opencv报错
- 如何查看MySQL源码
- 文件MD5查看linuxwindows
- php挂机源码,织音QQ助手QQ互赞挂机开源版源码
- Boost出现error C2678
- ctrl导致开机弹出计算机,电脑开机出现ctrl+alt+del是什么原因及解决方案
- 基于 mPaaS 框架 Portal-Bundle 接入方式下 Multidex 分包失效的解决方法