代码:
import win32gui
import win32con
import win32clipboard as w
import time
import requests
from lxml import etree
def request_page(session,url,headers):
response = session.get(url,headers=headers)
txt = response.text
return txt
def txt_to_html(txt)
html = etree.HTML(txt)
return html
def html_to_data(html,filter_condition=''):
span_data = html.xpath(filter_condition)
return span_data
if __name__ == '__main__':
url = 'https://cn.investing.com/currencies/aud-cny-historical-data/'
agent = ['Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
]
user_agent = agent[0]
headers = {
'Referer': 'https://www.baidu.com/',
'User-Agent': user_agent
}
session = requests.Session()
count = 0
while True:
txt = request_page(session,url,headers)
html = txt_to_html(txt)
data = html_to_data(html,filter_condition='//div[@class="float_lang_base_1 bold"]/span[@id="fl_header_pair_lst"]/text()')
data = float(data[0])
print(data,type(data))
if data <= 4.97:
msg = '汇率已经到达4.97'
name = "我的iphone"
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, msg)
w.CloseClipboard()
handle = win32gui.FindWindow(None, name)
win32gui.SendMessage(handle, 770, 0, 0)
win32gui.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
time.sleep(5)
count += 1
if count % 3 == 0:
user_agent = agent[1]
else:
user_agent = agent[0]
效果:
