因为学的是python2.7x。。。。
所以用的urllib
原来可以在submile 中运行Python代码。。。。。。。ctrl+b 就会在下面显示了
dir(urllib)就会显示这个模块的方法
help(urllib.open)就会显示这个方法的参数啊什么的
urlopen有3个参数,第一个url,第二个data,第三个代理
可以对一个对象进行dir,然后就能看到这个对象的可以用的方法了
原来写baidu.com 进入到了www.baidu.com是301重定向啊!!
403禁止访问
30x重定向
50x是服务器问题
python的urllib有一个直接把网页下载下来的!!!!方法
urllib.urlretrieve(url,’保存路径’)
urlretrieve
直接就下载了
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
一定要记得关闭啊!!!!close
另一种下载网页的方法
- 1
- 2
- 3
- 1
- 2
- 3
python 有个方法连用
- 1
- 2
- 3
- 1
- 2
- 3
要不然就要不写read(),然后在html = html.read()
如果只是进行逐个操作的话可以这么写
经常会出现编码错误,乱码,就要去看看网页是什么类型的编码,就是看源代码,然后看看是什么如 utf-8、GBK等等
然后在read()后就decode()把他解析了,然后再点encode()编码成自己想要的
可以在decode里面写(‘gbk’,’ignore’)ignore是忽略一些错误的编码,有可能一个网页中有好几种编码。
OS模块的getcwd()获取当前绝对路径,
chdir(‘另一个路径’) 切换到另一个路径
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
主要是那个函数。。。。。。
那个函数的3个函数,一个数据块的数量,数据块的大小,字节,数据文件大小
还有如果要输出百分号就要!!!!!!!写两个百分号
第三课,获取网页编码,获取的是网页头部的返回的
第三方模块,自动判断网页编码的库
下载chardet模块,安装,开始字符集检测,封装函数
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
那个info中的一个函数,获取编码类型
对网页的编码判断!!!
的第三方模块 chardet (字符集检测)
就是导入,然后 获取urlopen返回的在调用read返回的对象
用
chardet.detect(就是上面返回的对象)
就会返回是什么编码的概率
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
写了个函数
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
第四课
urllib2模块!!!!!!!!
一定要注意!国外和国内的编码,谷歌和百度,
GBK是中文!!!
编码的重要性!!!!!
破解不让抓取。。。
。。。。。CSDN就不行,会返回403
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
注意那个随机选择的是【】
先把url给request。然后添加头部信息,然后把返回的req放入urlopen中
第五课图片下载爬虫
例如去百度贴吧的图片
一定要写编码方式
那个-*-是直接就-就是不用加shift的
-- conding:utf-8 --
主要是学会了正则的一个方法findall(表达式,内容)会返回所匹配的内容
还有别傻逼似的什么都有正则写,,,,,,,,会变的用正则写,不变的就直接复制了
最好写成函数
我自己写的没有函数的提取快代理的代理ip和端口
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
第六课 用的第三方mokuai BeautifulSoup from bs4
很方便的就是不用写正则表达式,用的是beautifulsoup的,会根据html的标签来寻找,然后传入一个字符串也就是返回的网页,然后返回的是所有包含这个标签的,这个是第一个参数,第二个参数是class=‘’那个class要写成class_防止和python的类混,然后就会返回名字叫做这个的了,然后也是返回一堆,!!!返回一个对象,那就有方法,这个对象是一个类似字典的那种,那么查找src的那么就应该返回value了!于是返回的对象的[‘src’]那么就返回图片的网址了。当然可以写别的key来查找。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
判断文件是否存在
- 1
- 1
输出当前时间:格式化的
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
直接time.localtime返回的是tuple!!
我写的获取迅雷会员的代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
python编写 目录扫描工具
按行读取
- 1
- 2
- 3
- 1
- 2
- 3
那个splitext是把文件拆开,一个是路径+文件名,另一个是后缀名
!!!!!!!!!!!!!!!!!!!!!!!!!
。empty()是否为空
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
我肯了一个模拟表单提交的,抓包,看提交的data,直接创建字典data,可以自己设置参数修改等,然后创建heasers信息,也是抓包获取的,然后那个data要用urllib的
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
666的没边,我明白那个如果链接上了断线检测,设置时间每隔多久去ping下百度啊。。。
然后实现断线重现连接
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
http://cuiqingcai.com/2083.html
另一篇里讲的,关于正则,.*就是可以匹配任意无限个字符,加个?就是非贪婪模式
re.S代表在匹配时点任意匹配模式。点可以代表换行符
一般写正则都是先写compile,即
pattern = re.compile(r’想写的正则’)
然后
result = pattern.findall(要匹配的)
他设置了,输入,如果输入回车就索引加,然后就可以读下一个段子了
一直循环。。。。
一定要写异常!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
• re.I(全拼:IGNORECASE): 忽略大小写(括号内是完整写法,下同)
• re.M(全拼:MULTILINE): 多行模式,改变’^’和’$’的行为(参见上图)
• re.S(全拼:DOTALL): 点任意匹配模式,改变’.’的行为
• re.L(全拼:LOCALE): 使预定字符类 \w \W \b \B \s \S 取决于当前区域设定
• re.U(全拼:UNICODE): 使预定字符类 \w \W \b \B \s \S \d \D 取决于unicode定义的字符属性
• re.X(全拼:VERBOSE): 详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释。
创建目录要先看看是否存在这个目录
那个Request的三个参数是url,post数据,headers
转自:http://blog.csdn.net/qq_28295425/article/details/53729758