python正则表达式
python使用re模块提供对正则表达式的支持
import re
- 先将正则表达式的字符串形式编译成pattern实例
p = re.compile('hello')
- 使用pattern实例处理文本并获得匹配结果
m = p.match('hello world')
- 使用实例获得信息,进行其他操作
m.group() => 'hello'
或直接:
word = re.findall('hello','hello world') => ['hello']
字符
常用正则表达式
贪婪模式 (.+?)
匹配任意内容:非贪婪模式 (.?)
屏蔽中间多余代码 :[\s\S]?
匹配网址url:[a-zA-Z]+://[^\s]
匹配IP地址: \d+.\d+.\d+.\d+
匹配链接地址:href *= [’"](\S+)["’]
python web编程
相关模块 :urllib,urllib2,requests
urllib
url = ‘https://baidu.com’
r = urllib.urlopen(url)
print r.read() => 返回源代码
urllib2
与urllib相比没有urlretrieve(),多了个requests()
requests
未完待续…
ps: pycharm中对headers快速加引号
1. 先ctrl+f
2. 填入
(.*?):(.*)
'$1':'$2'
3. 勾选三个选项
4. 替换
文件按行去重
#coding=utf-8
#!/usr/bin/python
import shutil
a