import json
import tkinter as tk
import requests
def gui():
# 创建窗口界面
window = tk.Tk()
window.title("IP查地址")
window.geometry("200x100")
e = tk.Entry(window, show=None)
e.pack()
t = tk.Text(window, height=2)
t.pack()
def submit():
ip_str = str(e.get())
address = main(ip_str)
t.insert('end', address)
b = tk.Button(window, text='Submit', width=15, height=2, command=submit)
b.pack()
window.mainloop()
# 根据IP获得地址的主函数
def main(ip_str):
session = requests.Session()
url = 'https://tool.lu/ip/ajax.html'
url2 = 'https://tool.lu/ip'
add_ip = ip_str
form_data = {
'ip': add_ip
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36',
'cookie': 'uuid=74e87e7c-f499-494f-c9ae-0e67d9296946; slim_session=%7B%22slim.flash%22%3A%5B%5D%7D; Hm_lvt_0fba23df1ee7ec49af558fb29456f532=1574948551,1574959250; Hm_lpvt_0fba23df1ee7ec49af558fb29456f532=1574959250; _access=a407a34146823aa7d668beb57420b6ac839396c84224588886984b3cea946462'
}
r = session.get(url2)
# print(r.cookies.get_dict()['slim_session'])
r = session.post(url, headers=headers, data=form_data)
print(r.cookies.get_dict())
response = session.post(url, headers=headers, data=form_data)
add_dict = json.loads(response.text)
return add_dict['text']['chunzhen']
# print(add_dict)
# 刷新界面
def refresh():
url = 'https://tool.lu/ip'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36',
'cookie': 'uuid=74e87e7c-f499-494f-c9ae-0e67d9296946; Hm_lvt_0fba23df1ee7ec49af558fb29456f532=1574948551; slim_session=%7B%22slim.flash%22%3A%5B%5D%7D; _access=a407a34146823aa7d668beb57420b6ac2b2eeceeb56e6faa187c2e944580daa8; Hm_lpvt_0fba23df1ee7ec49af558fb29456f532=1574956537'
}
requests.get(url, headers=headers)
if __name__ == '__main__':
gui()
让我比较纳闷的事,网站的cookie一段时间就会改变,所以一段时间就要更改cookie,即使我采用了 requests.Session(),还是不行。
效果图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6D0ymLwC-1574960855658)(C:%5CUsers%5C10797%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1574960808436.png)]
了 requests.Session(),还是不行。
效果图