Python爬取新浪财经商品期权

编程入门 行业动态 更新时间:2024-10-26 07:24:23

Python爬取新浪财经商品<a href=https://www.elefans.com/category/jswz/34/1767698.html style=期权"/>

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爬取新浪财经商品期权

本文发布于:2024-03-09 14:50:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1725287.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:期权   新浪财经   商品   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!