码迷,mamicode.com
首页 > 其他好文 > 详细

requests 用法

时间:2018-08-16 00:54:23      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:with   font   load   agent   with open   代理   ida   int   ==   


# 百度
import requests

url = ‘http://www.baidu.com/‘

# requests 的 get用法
reponse = requests.get(url)

with open(‘baidu23.html‘, ‘wb‘) as f:
f.write(reponse.content)



# 百度翻译
import requests
import json

url = ‘http://fanyi.baidu.com/sug‘

def translate(kw):
form ={
‘kw‘:kw
}

# requests 的post 使用方法
response = requests.post(url, data=form)

response.encoding = ‘utf-8/gbk/gb2313/gb18080‘
str = response.text

# 解析json
res_dict = json.loads(str)
print(res_dict)

result = res_dict[‘data‘][0][‘v‘]
return result

if __name__ == ‘__main__‘:
res = translate(‘哈哈‘)
print(res)
res = translate(‘绿色‘)
print(res)



# 西祠代理
import requests

# url
url = ‘http://www.xicidaili.com‘
# 添加proxy 代理
proxy = {
‘http‘: ‘http://root:Yao+ql2011@101.200.50.18:8118‘
}
# 添加headers
headers = {
‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36‘
}

# 调用request., 得到response
response = requests.get(url, headers=headers, porxies=proxy)
print(requests.text)



# 百思不得姐
import requests

url = ‘http://www.budejie.com/‘

response = requests.get(url)

with open(‘baisi.html‘, ‘wb‘) as f:
f.write(response.content)

requests 用法

标签:with   font   load   agent   with open   代理   ida   int   ==   

原文地址:https://www.cnblogs.com/huangming17/p/9484330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!