期权"/>
Python爬取新浪财经商品期权
python爬取新浪财经商品期权并生成excel表格连接Navicat然后将数据存入数据库
为什么在做了东方财富网的大商所爬取代码后还要搞一个这个?
因为新浪财经的看涨合约和看跌合约是在一块的(相当于一次要爬取两个模块还要进行排版)
这里先给大家捋一下逻辑
1.我是先将爬取下来的数据存为txt文件(因为不知道怎么直接存为excel文件)
2.然后将txt文件转化为excel文件
3.将excel文件导入数据库
在做的时候,要注意变量的命名,不要重复了。。然后一直爆红改错(泪啊都是)
附上代码如下
import os
import timeimport openpyxl
import pymysql
import xlrd
from selenium import webdriverresponse = webdriver.Chrome()# 获取网页地址
url = ".php/m_o/dce"response.get(url)a = ['m2101','m2105','m2011','m2012','m2109','m2103']
s = 1# 连接数据库
try:db = pymysql.connect(host='rm-wz95oz7m93cejtuko4o.mysql.rds.aliyuncs', port=3306, user='super', password='super888', db='financial')
except:print("could not connect to mysql server")# 定义爬取数据代码
def crash():a = response.find_element_by_xpath("//*[@class='table_up fl']")a1 = response.find_element_by_xpath("//*[@class='table_down fr']")b = a.texttime.sleep(1)c = b.split("\n")b1 = a1.texttime.sleep(1)c1 = b1.split("\n")# 因为看涨和看跌数量不一样,所以设ifif len(c1) < len(c):for i in range(len(c1)):s = c[i] + ' ' + c1[i] + "\n"time.sleep(1)with open(file_path, "a", encoding="utf-8") as fp:fp.write(s)for i in range</
更多推荐
Python爬取新浪财经商品期权
发布评论