python requests库的简单运用
python
requests的简单运用
使用pycharm获取requests包
ctrl+alt+s Project:pythonProject pythoninterpreter 点+号搜索
使用requests里的socket达成局域网内通讯,UDP
import time #server
import socket
try:
s = socket.socket(type=socket.SOCK_DGRAM) # 创建类
hostname = socket.gethostname() # 获取自己的主机名
s.bind((hostname, 8888)) # 绑定主机号与端口号
msg,address=s.recvfrom(1024)
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())),
"服务端收到客户端:%s的消息:%s"%(address,msg.decode('utf-8')))
data='可以正常通讯'.encode('utf-8')
s.sendto(data,address)
while True:
a = input("发送消息(q = quit)")
if a != "q":
s.sendto(a.encode('utf-8'), address)
else:
break
msg,address=s.recvfrom(1024)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())),
"服务端收到客户端:%s的消息:%s" % (address, msg.decode('utf-8')))
s.close()
except Exception as e:
print("出错了", e)
import socket #client
import time
try:
s=socket.socket(type=socket.SOCK_DGRAM)
hostname=socket.gethostname()
data='连接成功'.encode('utf-8')
s.sendto(data,(hostname,8888))
responese,address=s.recvfrom(1024)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),
'收到服务端%s的消息:%s'%(address,responese.decode('utf-8')))
n=0
while n<1000:
response,address = s.recvfrom(1024)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())),
"收到服务端%s的消息:%s" % (address, response.decode('utf-8')))
a = input('发送信息')
s.sendto(a.encode('utf-8'),address)
s.close()
except Exception as e:
print('出错了',e)
使用requests里的socket达成局域网内通讯,TCP
import socket #server
import time
try:
s = socket.socket()
hostname = socket.gethostname()
# 绑定套接字地址
s.bind((hostname, 8888))
s.listen(5)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '服务端准备完毕,等待客户端连接')
con, address = s.accept()
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),'客户端已连接上服务端,连接地址:', address)
message = '你好,我是服务端'
# 发送消息,必须对发送的内容进行编码
con.send(message.encode('utf-8'))
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '服务端发送:', message)
# 关闭套接字
con.close()
except Exception as e:
print('建立TCP服务端失败', e)
import socket
import time
try:
s = socket.socket()
hostname = socket.gethostname()
s.connect((hostname, 8888))
response = s.recv(1024).decode('utf-8') # 接收服务端消息并解码
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '收到服务端消息:', response)
s.close() # 关闭套接字
except Exception as e:
print('建立TCP客户端失败', e)
requests模块里的get应用
import requests
word = input('请输入搜索内容')
pn = input('请输入页码')
p={'kw':word,"pn":pn}
url='https://tieba.baidu.com/f'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0'}
re=requests.get(url,headers=headers,params=p)
print(re.content.decode('utf-8'))
输入图片地址将结尾改成.jpg可以爬图片
request模块里的post应用
这里使用post去给百度翻译传参数
import requests
import json
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"}
url='https://fanyi.baidu.com/sug'
while True:
word = input('百度翻译:')
if word.lower() == 'quit':
break
data={"kw":word}
re=requests.post(url,headers=headers,data=data) #地址,伪装的浏览器以及数据
re_obj=re.json() #把json格式写进re_obj
print(word+":"+re_obj['data'][0]['v']) #指定re_obj里的特定值
filename=word+'.json' #设置文件名
fp=open(filename,"w",encoding='utf-8') #以utf-8写入文件
json.dump(re_obj,fp=fp,ensure_ascii=False) #不允许json以ascii码的形式写入文件
get是在url地址上加参数,post是在页面里面的文本栏加参数