urllib
Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了。
它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务,
下面是简单的使用urllib来进行请求数据的方法
import urllib.request f=urllib.request.urlopen('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508') result = f.read().decode('utf-8') # 或者 import urllib.request req = urllib.request.Request("http://www.baidu.com") response = urllib.urlopen(req) print(response.read().decode('utf-8'))
我们更推荐大家使用第二种方法,两种方法请求的结果都一样,只不过第二种中间多了一个request对象,为啥要这样子,因为在构建请求时还需要加入好多内容,因此通过构建一个request,服务器响应请求得到应答,这样显得逻辑上清晰明确
比如说加入User-Agent参数到请求头,就可以使用如下方式
import urllib.request req = urllib.request.Request('http://www.example.com/') req.add_header("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0") r = urllib.request.urlopen(req) result = f.read().decode('utf-8')
Python官方文档:https://docs.python.org/3.5/library/urllib.request.html#module-urllib.request
Requests
Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,
从而使得进行网络请求时,变得美好了许多,而且使用Requests可以轻而易举的完成浏览器可有的任何操作
1.安装模块
pip3 install requests
2.简单使用
import requests r = requests.get('http://www.shangzekai.xyz') print type(r) print r.status_code # 服务器返回状态码 print r.encoding # 使用的编码 print r.text # 返回的内容
cookies ...
3.基本方法
3.1 get请求
# 1、无参数实例 import requests ret = requests.get('https://github.com/timeline.json')
print(ret.url) print(ret.text) # 2、有参数实例 import requests payload = {'key1': 'value1', 'key2': 'value2'} ret = requests.get("http://httpbin.org/get", params=payload) print(ret.url) print(ret.text)
# 3、解析json import requests import json response = requests.get("http://httpbin.org/get") print(type(response.text)) print(response.json()) print(json.loads(response.text)) print(type(response.json()))
# 4、添加headers import requests response = requests.get("https://www.zhihu.com/explore") print(response.text) 此时,我们需要加上一些头信息,来模拟浏览器的登录 import requests headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' } response = requests.get("https://www.zhihu.com/explore", headers=headers) print(response.text)
3.2 Post 请求
# 1、基本POST实例 import requests # 当headers为application/content的时候,请求实例如下: payload = {'key1': 'value1', 'key2': 'value2'} ret = requests.post("http://httpbin.org/post", data=payload) print(ret.text) print(type(response.headers), response.headers) print(type(response.cookies), response.cookies) print(type(response.url), response.url) print(type(response.history), response.history) # 2、发送请求头和数据实例 import json import requests url = 'http://httpbin.org/post' payload = {'some': 'data'} headers = {'content-type': 'application/json'} # 当headers为application/json的时候,请求示例如下: ret = requests.post(url, data=json.dumps(payload), headers=headers) print(ret.text)
注意
get和post请求两者的区别在于,get请求方法参数只有params,而没有data参数,而post请求中两者都是有的

生成User-Agent
from fake_useragent import UserAgent ua = UserAgent() headers = { 'User-Agent' : ua.random }
4 模拟登陆
# 1. 首先如何获取cookie import requests response = requests.get('http://www.baidu.com') print(response.cookies) for key,value in response.cookies.items(): print(key + '=' + value) # 2. 会话登录 import requests ## 表示设置一个cookie requests.get('http://www.httpbin.org/cookies/set/123456789') ## 然后获取网站的cookie res = requests.get('http://www.httpbin.org/cookies') print(res.text) 打印,我们发现没有数据,因为上面的两行代码,就相当于两个浏览器进行访问,因此不可能获取到第一次cookie访问的信息 因此,我们采用如下的方法进行模拟登陆 s = requests.Session() requests.get('http://www.httpbin.org/cookies/set/123456789') res = requests.get('http://www.httpbin.org/cookies') print(res.text) 最终,我们采用上述的方法,获取到了最终的cookie的值,获取该值之后,我们可以拿着个cookie来进行登录了
5) SSL设置
import requests from requests.packages import urllib3 urllib3.disable_warnings() res = requests.get('http://www.12306.cn',verify=False) print(res.status_code) import requests response = requests.get('https://www.12306.cn', cert=('/path/server.crt', '/path/key')) print(response.status_code)
* requests其他请求
requests.get(url, params=None, **kwargs) requests.post(url, data=None, json=None, **kwargs) requests.put(url, data=None, **kwargs) requests.head(url, **kwargs) requests.delete(url, **kwargs) requests.patch(url, data=None, **kwargs) requests.options(url, **kwargs) # 以上方法均是在此方法的基础上构建 requests.request(method, url, **kwargs)
6) 代理设置
import requests proxies = { "http": "http://127.0.0.1:9743", "https": "https://127.0.0.1:9743", } response = requests.get("https://www.taobao.com", proxies=proxies) print(response.status_code)
有密码的设置
import requests proxies = { "http": "http://user:password@127.0.0.1:9743/", } response = requests.get("https://www.taobao.com", proxies=proxies) print(response.status_code)
7)、超时时间设置
import requests from requests.exceptions import ReadTimeout try: response = requests.get("http://httpbin.org/get", timeout = 0.5) print(response.status_code) except ReadTimeout: print('Timeout')
8)、异常处理 (导入模块)
import requests from requests.exceptions import ReadTimeout, ConnectionError, RequestException try: response = requests.get("http://httpbin.org/get", timeout = 0.5) print(response.status_code) except ReadTimeout: print('Timeout') except ConnectionError: print('Connection error') except RequestException: print('Error')
`常见实例
1) 检测QQ是否在线
import urllib import requests from xml.etree import ElementTree as ET # 使用内置模块urllib发送HTTP请求,或者XML格式内容 """ f = urllib.request.urlopen('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=493133139') result = f.read().decode('utf-8') """ # 使用第三方模块requests发送HTTP请求,或者XML格式内容 r = requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=493133139') result = r.text # 解析XML格式内容 node = ET.XML(result) # 获取内容 if node.text == "Y": print("在线") else: print("离线")
2) 查看火车停靠信息
import urllib import requests from xml.etree import ElementTree as ET # 使用内置模块urllib发送HTTP请求,或者XML格式内容 """ f = urllib.request.urlopen('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=') result = f.read().decode('utf-8') """ # 使用第三方模块requests发送HTTP请求,或者XML格式内容 r = requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=') result = r.text # 解析XML格式内容 root = ET.XML(result) for node in root.iter('TrainDetailInfo'): print(node.find('TrainStation').text,node.find('StartTime').text,node.tag,node.attrib)
Python -- 正则
1.正则表达式的概念
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑
正则表达式的大致匹配过程是:
1.依次拿出表达式和文本中的字符比较,
2.如果每一个字符都能匹配,则匹配成功;一旦有匹配不成功的字符则匹配失败。
3.如果表达式中有量词或边界,这个过程会稍微有一些不同
2.常见的正则表达式符号
'^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a"," abc eee",flags=re.MULTILINE) '$' 匹配字符结尾 '*' 匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac") 结果为['abb', 'ab', 'a'] '+' 匹配前一个字符1次或多次,re.findall("ab+","abcdabbbba") 结果['ab', 'abb'] '?' 匹配前一个字符1次或0次 '.' 默认匹配除 之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '{m}' 匹配前一个字符m次 '{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb'] '|' 匹配|左或|右的字符,re.findall("abc|ABC","ABCBabcCD")结果'ABC' '(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c 'A' 只从字符开头匹配 '' 匹配字符结尾,同$ 'd' 匹配数字0-9 'D' 匹配非数字 'w' 匹配[A-Za-z0-9] 'W' 匹配非[A-Za-z0-9] 's' 匹配空白字符空格、 、 、 '(?P<name>...)' 分组匹配 re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict("city") 结果{'province': '3714', 'city': '81', 'birthday': '1993'}
常见的使用方法
- re.findall 把所有匹配到的字符放到列表中,以列表中的元素返回
findall(pattern, string, flags=0)
print(re.findall('d+','one1two2three3four4'))
3.贪婪模式和非贪婪模式
Python里数量词默认是贪婪的,总是尝试匹配尽可能多的字符;
非贪婪则相反,总是尝试匹配尽可能少的字符
但有的时候,我们并不是想要贪婪模式,那怎么办?
非常简单,只需要在”*”,”?”,”+”,”{m,n}”后面加上?,使贪婪变成非贪婪
>>> s="This is a number 234-235-22-423" >>> r=re.findall(".+(d+-d+-d+-d+)",s) >>> r '4-235-22-423' >>> r=re.findall(".+?(d+-d+-d+-d+)",s) >>> r '234-235-22-423' >>>
解决方式:非贪婪操作符“?”,这个操作符可以用在”*”,”+”,”?”的后面,要求正则匹配的越少越好
>>> re.findall(r"aa(d+)","aa2343ddd") ['2343'] >>> re.match(r"aa(d+?)","aa2343ddd").group(1) '2'
4.Python的多行匹配和点任意匹配
r=re.complile(pattern,re.M)
re.M(re.MULTILINE):多行模式,改变’^’和’$
‘的行为,即^ $标志将会匹配每一行
>>> re.findall(r"^a(d+)b","a213b a2345b a234b") ['213'] >>> re.findall(r"^a(d+)b","a213b a2345b a234b",re.M) ['213', '2345', '234'] >>> re.findall(r"a(d+)b","a213b a2345b a234b") #如果没有^标志,无需re.M ['213', '2345', '234']
re.S(re.DOTALL):点任意匹配模式
元字符“.”在默认模式下,匹配除换行符外的所有字符。在DOTALL模式下,匹配所有字符,包括换行符
>>> re.findall(r"."," ",re.S) [' ']
5.常见的正则表达式
IP: ^(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}$ 手机号: ^1[3|4|5|8][0-9]d{8}$ 邮箱: [a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+
6.一个简单的抓取网页分析的案例
import requests import re hds=[{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}, {'User-Agent':'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11'}, {'User-Agent':'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'}, {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0'}, {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36'}, {'User-Agent':'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'}, {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'}, {'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'}, {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'}, {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'}, {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'}, {'User-Agent':'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11'}, {'User-Agent':'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11'}] def get_page(url): headers = hds[random.randint(0,len(hds)-1)] response = requests.get(url, headers = headers) try: if response.status_code == 200: res = response.text return res return None except Exception as e: print(e) ''' <div class="board-item-content"> <div class="movie-item-info"> <p class="name"><a href="/films/1203" title="霸王别姬" data-act="boarditem-click" data-val="{movieId:1203}">霸王别姬</a></p> <p class="star"> 主演:张国荣,张丰毅,巩俐 </p> <p class="releasetime">上映时间:1993-01-01(中国香港)</p> </div> <div class="movie-item-number score-num"> <p class="score"><i class="integer">9.</i><i class="fraction">6</i></p> </div> </div> ''' def get_movie(html): partten = '<p.*?><a.*?>(.*?)</a></p>.*?<p.*?>(.*?)</p>.*?<p.*?>(.*?)</p>' items = re.findall(partten, html, re.S) #print((items)) return items def write_file(items): fileMovie = open('movie.txt', 'w', encoding='utf8') try: for movie in items: fileMovie.write('电影排名:' + movie[0] + ' ') fileMovie.write('电影主演:' + movie[1].strip() + ' ') fileMovie.write('上映时间:' + movie[2] + ' ') print('文件写入成功...') finally: fileMovie.close() def main(url): html = get_page(url) items = get_movie(html) write_file(items) if __name__ == '__main__': url = "http://maoyan.com/board/4" main(url)