python爬取东方财富网历史资金流向(已存入MySQL)

news/2024/7/19 8:40:53 标签: mysql, python, 爬虫, 数据类型
from selenium import webdriver  #导入模块,selenium导入浏览器驱动,用get方法打开浏览器
import time
import re
import csv  #存储数据
from lxml import etree #数据的解析
import pandas as pd
import numpy as np
option = webdriver.ChromeOptions()   #网址获取
option.add_argument('headless')  #无界面启动,即设置浏览器静默
driver = webdriver.Chrome(options=option)
driver.get('http://data.eastmoney.com/zjlx/000040.html')
table=driver.find_element(By.ID,'table_ls')
# data_heads1=table.find_elements(By.TAG_NAME,'th')[:8]
# data_heads2=table.find_elements(By.TAG_NAME,'th')[8:]
data_bodies = table.find_element(By.TAG_NAME,'tbody').find_elements(By.TAG_NAME,'tr')
datas=[]
for data_body in data_bodies:
    data3=data_body.text.split()
    datas.append(data3)
datas
name=['日期','收盘价','涨跌幅','主力净流入净额','主力净流入净占比','超大单净流入净额','超大单净流入净占比',
      '大单净流入净额','大单净流入净占比','中单净流入净额','中单净流入净占比','小单净流入净额','小单净流入净占比']
data= pd.DataFrame(columns=name,data=datas,dtype=np.float64)
data
from sqlalchemy import create_engine 
import pymysql
conn= pymysql.connect(host='localhost',port=3306,user='root',passwd='root',
                             db='mysql',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)
engine=create_engine('mysql+pymysql://root:root@localhost:3306/mysql?charset=utf8mb4')
df=pd.DataFrame(data)
df.日期=pd.to_datetime(df.日期,format='%Y-%m-%d')
with engine.begin()as conn:
    df.to_sql(name='economic',con=conn,if_exists='replace',index=False)
conn.close()

疑问:我的变量类型尝试了很多方法去转换,但是始终存到MySQL里面的时候其他变量都是text类型,且无法显示具体数字;而且就算在jupyter里面也是无论怎么转换类型,结果输出来我的变量类型都是object(日期跟收盘价除外,但是日期的类型导致我在SQL里面会显示时间,我只想有日期不想有时间该怎么弄啊)

上述问题已解决,需要对MySQL进行utf8编码,完整代码如下:

from selenium import webdriver  #导入模块,selenium导入浏览器驱动,用get方法打开浏览器
import time
import re
import csv  #存储数据
from lxml import etree #数据的解析
import pandas as pd
import numpy as np
from selenium.webdriver.common.by import By
option = webdriver.ChromeOptions()   #网址获取
# option.add_argument('headless')  #无界面启动,即设置浏览器静默
# driver = webdriver.Chrome(options=option)
driver = webdriver.Chrome()
driver.get('http://data.eastmoney.com/zjlx/000040.html')
type(driver)
table=driver.find_element(By.ID,"table_ls")
table.text
# data_heads1=table.find_elements(By.TAG_NAME,'th')[:8]
# data_heads2=table.find_elements(By.TAG_NAME,'th')[8:]
data_bodies = table.find_element(By.TAG_NAME,'tbody').find_elements(By.TAG_NAME,'tr')
datas=[]
for data_body in data_bodies:
    data3=data_body.text.split()
    datas.append(data3)
datas
name=['日期','收盘价','涨跌幅','主力净流入净额','主力净流入净占比','超大单净流入净额','超大单净流入净占比',
      '大单净流入净额','大单净流入净占比','中单净流入净额','中单净流入净占比','小单净流入净额','小单净流入净占比']
data= pd.DataFrame(columns=name,data=datas,dtype=np.float64)
data
#以下操作准备连接mysql
import sqlalchemy
from sqlalchemy import create_engine 
import pymysql
conn= pymysql.connect(host='localhost',port=3306,user='root',passwd='root',
                             db='mysql',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)
engine=create_engine('mysql+pymysql://root:root@localhost:3306/mysql?charset=utf8mb4')
df=pd.DataFrame(data)
df.日期=pd.to_datetime(df.日期,format='%Y-%m-%d')
with engine.begin()as conn:
    df.to_sql(name='economic', con=conn,if_exists='replace',index=False,
         dtype={'收盘价': sqlalchemy.types.String(length=20),
       '涨跌幅': sqlalchemy.types.String(length=20),
       '主力净流入净额': sqlalchemy.types.String(length=20),
       '主力净流入净占比':  sqlalchemy.types.String(length=20),
       '超大单净流入净额':  sqlalchemy.types.String(length=20),
       '超大单净流入净占比':  sqlalchemy.types.String(length=20),
       '大单净流入净额':  sqlalchemy.types.String(length=20),
       '大单净流入净占比':  sqlalchemy.types.String(length=20),
       '中单净流入净额':  sqlalchemy.types.String(length=20),
       '中单净流入净占比':  sqlalchemy.types.String(length=20),
       '小单净流入净额':  sqlalchemy.types.String(length=20),
       '小单净流入净占比':  sqlalchemy.types.String(length=20),
       })
conn.close()



http://www.niftyadmin.cn/n/1264826.html

相关文章

Lecode 684. 冗余连接 并查集

原题链接&#xff1a;Lecode 684. 冗余连接 并查集 class Solution { public:map<int,int> father;int findfather(int x){if(father[x]x) return x;else return findfather(father[x]);}void Union(int x,int y){int fxfindfather(x);int fyfindfather(y);if(fx!fy) …

Leecode 802. 找到最终的安全状态 拓扑排序/DFS

原题链接&#xff1a;802. 找到最终的安全状态 拓扑排序 class Solution { public:vector<int> eventualSafeNodes(vector<vector<int>>& graph) {vector<int> res;int ngraph.size();vector<int> degree(n);vector<vector<int>…

泰坦尼克号入门学习(思维导图)

import numpy as np import pandas as pd import seaborn as sns from sklearn import metrics import matplotlib.pyplot as plt datapd.read_csv(rC:\Users\BX田田\Desktop\数据挖掘\Titanic\data.csv,encodingUTF-8) #data.csv是泰坦尼克的训练集数据 data[Age]data[Age].…

Leecode 785. 判断二分图 DFS染色/BFS

原题链接&#xff1a;Leecode 785. 判断二分图 BFS class Solution { public:bool isBipartite(vector<vector<int>>& graph) {int ngraph.size();vector<int> color(n,0);queue<int> q;for(int i0;i<n;i){if(!color[i]){color[i]1;q.push…

统计学习基础--第一、二章 导论

一、data的理解 我们把data分为训练集和测试集&#xff0c;其中训练集用于建立模型&#xff0c;通常要占data的80%&#xff1b;而测试集则是用于预测分析&#xff0c;观察拟合出的模型的效果。 二、数据预处理 1、处理数据文件格式&#xff1b; 2、观察数据是否有缺失值或异…

Leecode 886. 可能的二分法 DFS染色/BFS

原题链接&#xff1a;Leecode 886. 可能的二分法 和这道题大同小异&#xff1a;Leecode 785. 判断二分图 DFS染色/BFS BFS class Solution { public:bool possibleBipartition(int n, vector<vector<int>>& dislikes) {vector<int> color(n1,0);queue&…

统计学习基础--第三章 线性回归

目录 一、简单线性回归 1、表达式 2、估计系数 &#xff08;1&#xff09;方法&#xff1a;最小二乘法 &#xff08;2&#xff09;实质&#xff1a;​ &#xff08;3&#xff09;结果 &#xff08;4&#xff09;评估系数估计的准确性 3、评估模型的准确性 二、多元线性回…

统计学习基础--第四章 分类

目录 一、逻辑斯谛回归&#xff08;logistic&#xff09; 1、Logistic模型 &#xff08;1&#xff09;概率 &#xff08;2&#xff09;逻辑斯谛函数 &#xff08;3&#xff09;注意 2、估计回归系数 &#xff08;1&#xff09;方法&#xff1a;极大似然估计 &#xff0…