requests 用法


# 百度
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:[email protected]: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)

猜你喜欢

转载自www.cnblogs.com/huangming17/p/9484330.html