网站爬虫获取实时汇率
下面给出一个函数包,直接调用即可
def Exchange_Rate_Realtime_By_self(url_choose, currency_name, number_s): # 自定义函数 url_choose = http://webforex.hermes.hexun.com/forex/quotelist?code=FOREXUSDCNY&column=Code,Price 为美元价格
'''
url_choose = "http://webforex.hermes.hexun.com/forex/quotelist?code=FOREXUSDCNY&column=Code,Price" 为美元价格
url_choose = "http://webforex.hermes.hexun.com/forex/quotelist?code=FOREXGBPCNY&column=Code,Price" 为英镑价格
'''
# 验证请求信息
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
# 爬取实时美元汇率数据
chaper_url = url_choose
req = urllib.request.Request(url=chaper_url, headers=headers)
f = urllib.request.urlopen(req)
html = f.read().decode("utf-8")
print(html)
s = re.findall("{.*}", str(html))[0]
sjson = json.loads(s)
USDCNY = sjson["Data"][0][0][1] / int(number_s)
print('实时%r价格: %r' % (currency_name, USDCNY))
return USDCNY/10000
"
如果世界上有什么奇迹
一定是尽力理解某个人
并与之同甘共苦
"