python安全工具开发应用02Web编程

01 三个库  urllib uillib2  requests

urllib.urlopen()

urllib2.urlopen()

urllib.urlopen()

综合一下:目前而言我们只能用python3的urllib

实例:

 
#方法一:
import urllib.request
request_url = 'http://www.baidu.com'           # 需要请求的URL地址
response = urllib.request.urlopen(request_url) # 发起请求
print(response.read().decode('utf-8'))         # 打印响应的文本,并进行UTF-8解码
#方法二:
//利用Python 抓取指定页面
import urllib.request
url = "http://www.baidu.com"
data = urllib.request.urlopen(url).read()
data = data.decode('UTF-8')
print(data)

#下载图片
import urllib.request
urllib.request.urlretrieve('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529243320258&di=0bedf00ad8d1f35a1b7538548a3679a2&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F95eef01f3a292df59fc681e5bc315c6034a8733c.jpg',filename='H:\Qt\1.png')



request模块   这个一旦使用不再愿意使用urllib

1.安装:          pip install requests

2.发送网络请求    >>r=requests.get(url)

                      还可以是post/put/delete/head/options

3.为url传递参数    >>payload={'key1':'value','key2':'value2'}

                               >>requests.get(url,params=payload)

                                >>print(r.url)

4.相应内容       >> r=requests.get(url)

                        >>r.text

                        >>r.encoding 'utf-8'

                        >>r.encoding='ISO -8859-1'

5.二进制相应内容

                             >>payload={'key1':'value','key2':'value2'}

                               >>requests.get(url,params=payload)


6.定制请求头

                        >>url=''

                        >>head={'content-type':'application/ison'}

                        >>r=requests.get(url,head=headers)

7.复杂的post请求

                       >>payload={'key1':'value','key2':'value2'}

                        >>requests.get(url,params=payload)

8.响应状态码

                        >>r=requests.get(url)

                        >>r.status_code

                200

9.响应头

                        >>r.headers

10.Cookies

                        >>r.cookies

                         >>r.cookies['example cooke name']

 11.超时

                           >>requests.get(url,timeout=0.001)

12.错误和异常

                遇到网络问题(如:DNS查询失败,拒绝连接等时,requests会抛出一个ConnectionError异常

                HTTP无效响应时,     HTTPError

                请求超时,    Timeout异常




02爬虫的介绍

网页获取信息/目录扫描/扫描存在缺陷/

#coding=utf-8
import requests
import json
url ="https://www.ichunqiu.com/courses/qyaqll"
headers={

}
r = requests.get(url=url,headers=headers)
print(r.text)

data =json.load(r.text)
print(data)


03利用python开发一个爬虫

可利用for循环

  1. #coding:utf8  
  2.   
  3. import urllib2  
  4. #json解析库,对应到lxml  
  5. import json  
  6. #json的解析语法,对应到xpath  
  7. import jsonpath  
  8.   
  9. url="http://www.lagou.com/lbs/getAllCitySearchLabels.json"  
  10. header={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"}  
  11.   
  12. request=urllib2.Request(url,headers=header)  
  13.   
  14. response=urllib2.urlopen(request)  
  15. #取出json文件里的内容,返回的格式是字符串  
  16. html=response.read()  
  17.   
  18. #把json形式的字符串转换成python形式的Unicode字符串  
  19. unicodestr=json.loads(html)  
  20.   
  21. #python形式的列表  
  22. city_list=jsonpath.jsonpath(unicodestr,"$..name")  
  23.   
  24. #打印每个城市  
  25. for i in city_list:  
  26.     print i  
  27.   
  28. #dumps()默认中文伟ascii编码格式,ensure_ascii默认为Ture  
  29. #禁用ascii编码格式,返回Unicode字符串  
  30. array=json.dumps(city_list,ensure_ascii=False)  
  31.   
  32. #把结果写入到lagouCity.json文件中  
  33. with open("lagouCity.json","w") as f:  
  34.     f.write(array.encode("utf-8")) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值