文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Python爬虫之Requests库的基

2023-01-30 22:47

关注
  1 import requests
  2 response = requests.get('http://www.baidu.com/')
  3 print(type(response))
  4 print(response.status_code)
  5 print(type(response.text))
  6 print(response.text)
  7 print(response.cookies)
  8 
  9 # 各种请求方式
 10 import requests
 11 requests.post('http://httpbin.org/post')
 12 requests.put('http://httpbin.org/put')
 13 requests.delete('http://httpbin.org/delete')
 14 requests.head('http://httpbin.org/get')
 15 requests.options('http://httpbin.org/get')
 16 
 17 # 基本GET请求
 18 import requests
 19 response = requests.get('http://httpbin.org/get')
 20 print(response.text)
 21 
 22 # 带参数GET请求
 23 import requests
 24 response = requests.get('http://httpbin.org/get?name=germey&age=22')
 25 print(response.text)
 26 
 27 import requests
 28 data = {
 29     'name': 'germey',
 30     'age': 22
 31 }
 32 response = requests.get('http://httpbin.org/get', params = data)
 33 print(response.text)
 34 
 35 # 解析json
 36 import requests
 37 import json
 38 response = requests.get('http://httpbin.org/get')
 39 print(type(response.text))
 40 print(response.json())
 41 print(json.loads(response.text))
 42 print(type(response.json()))
 43 
 44 # 获取二进制数据
 45 import requests
 46 response = requests.get('http://github.com/favicon.ico')
 47 print(type(response.text), type(response.content))
 48 print(response.text)
 49 print(response.content)
 50 
 51 # 保存图片
 52 import requests
 53 response = requests.get('http://github.com/favicon.ico')
 54 with open('1.ico', 'wb') as f:
 55     f.write(response.content)
 56     f.close()
 57 
 58 # 添加headers 不添加的话会请求失败的
 59 import requests
 60 response = requests.get('http://www.zhihu.com/explore')
 61 print(response.text)
 62 
 63 import requests
 64 headers = {
 65     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
 66 }
 67 response = requests.get('http://zhihu.com/explore', headers = headers)
 68 print(response.text)
 69 
 70 # 基本的POST请求
 71 import requests
 72 data = {'name': 'germey', 'age': 22}
 73 response = requests.post('http://httpbin.org/post', data = data)
 74 print(response.text)
 75 
 76 import requests
 77 data = {'name':'germey', 'age':22}
 78 headers = {
 79     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
 80 }
 81 response = requests.post('http://httpbin.org/post', data = data, headers = headers)
 82 print(response.json())
 83 
 84 # response属性
 85 import requests
 86 response = requests.get('http://www.jianshu.com')
 87 print(type(response.status_code), response.status_code)
 88 print(type(response.headers), response.headers)
 89 print(type(response.cookies), response.cookies)
 90 print(type(response.url), response.url)
 91 print(type(response.history), response.history)
 92 
 93 # 文件上传
 94 import requests
 95 files = {'file':open('1.ico', 'rb')}
 96 response = requests.post('http://httpbin.org/post', files = files)
 97 print(response.text)
 98 
 99 # 获取cookie
100 import requests
101 response = requests.get('http://www.baidu.com')
102 print(response.cookies)
103 for key, value in response.cookies.items():
104     print(key + ' = ' + value)
105 
106 # 会话维持 模拟登陆
107 import requests
108 requests.get('http://httpbin.org/cookies/set/number/123456789')
109 response = requests.get('http://httpbin.org/cookies')
110 print(response.text)
111 
112 import requests
113 s = requests.session()
114 s.get('http://httpbin.org/cookies/set/number/123456789')
115 response = s.get('http://httpbin.org/cookies')
116 print(response.text)
117 
118 # 证书验证
119 import requests
120 response = requests.get('https://www.12306.cn')
121 print(response.status_code)

 

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯