用python网络爬虫写一份程序,实现抓取招商银行外汇网站:http://fx.cmbchina.com/提取外汇数据,数据输出(数据包含:现汇卖出价,现钞卖出价,现汇买入价,现钞买入价)使用反爬虫技术,并将抓取的数据显示到IU界面中)
时间: 2024-04-03 09:35:11 浏览: 120
招商银行网上银行业务的竞争力发展分析.pdf
好的,这是一个比较复杂的任务,我们需要使用 requests、BeautifulSoup、re 和 tkinter 这些库来实现。以下是代码实现:
```python
import requests
from bs4 import BeautifulSoup
import re
import tkinter as tk
# 反爬虫技术:设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送请求并获取页面内容
def get_html(url):
try:
r = requests.get(url, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
# 解析页面内容并提取所需数据
def get_data(html):
soup = BeautifulSoup(html, "html.parser")
pattern = re.compile(r'\d+\.\d+')
data = {}
tags = soup('td')
for tag in tags:
if 'class' in tag.attrs and 'fxsightCur' in tag.attrs['class']:
if 'USD' in tag.attrs['class']:
data['USD'] = pattern.findall(tag.string)
elif 'JPY' in tag.attrs['class']:
data['JPY'] = pattern.findall(tag.string)
elif 'EUR' in tag.attrs['class']:
data['EUR'] = pattern.findall(tag.string)
elif 'GBP' in tag.attrs['class']:
data['GBP'] = pattern.findall(tag.string)
elif 'HKD' in tag.attrs['class']:
data['HKD'] = pattern.findall(tag.string)
elif 'CAD' in tag.attrs['class']:
data['CAD'] = pattern.findall(tag.string)
elif 'AUD' in tag.attrs['class']:
data['AUD'] = pattern.findall(tag.string)
elif 'SGD' in tag.attrs['class']:
data['SGD'] = pattern.findall(tag.string)
return data
# 构建 GUI 界面
def show_data():
url = 'http://fx.cmbchina.com/'
html = get_html(url)
data = get_data(html)
window = tk.Tk()
window.title('招商银行外汇数据')
window.geometry('450x300')
tk.Label(window, text='货币种类').grid(row=0, column=0)
tk.Label(window, text='现汇卖出价').grid(row=0, column=1)
tk.Label(window, text='现钞卖出价').grid(row=0, column=2)
tk.Label(window, text='现汇买入价').grid(row=0, column=3)
tk.Label(window, text='现钞买入价').grid(row=0, column=4)
row = 1
for key in data:
col = 0
tk.Label(window, text=key).grid(row=row, column=col)
for val in data[key]:
col += 1
tk.Label(window, text=val).grid(row=row, column=col)
row += 1
window.mainloop()
# 运行程序
if __name__ == '__main__':
show_data()
```
运行程序后,会弹出一个 GUI 界面,显示当前的外汇数据。
阅读全文