Home > other >  Novice [question] how each cycle will content into excel, cannot cover the original content
Novice [question] how each cycle will content into excel, cannot cover the original content

Time:03-12

The import re
The from the selenium. Webdriver. Support the import expected_conditions as EC
The from selenium.webdriver.com mon. By the import by
The from the selenium import webdriver
The from the selenium. Webdriver. Support. Wait the import WebDriverWait
The import pandas as pd
The import numpy as np
The import time



Only_num=re.com running (' (\ d +) (\ d +) ')
writer=pd. ExcelWriter (" stock_info. XLS ")



Browser_code=webdriver. Chrome ()
Browser_code. Get (" https://hk9999e.com/? Jrh925 #/stockmarket ")
List_stock_codes=[]
For page_code in range (1, 3) :
Xpath_page="/HTML/body/div/div [1]/div [2]/div/div [2]/div/div [2]/div [2]/div/ul/li [% d]" % page_code
Stock_page=browser_code. Find_element_by_xpath (xpath_page). Click ()
For get_code in range (1 dec) :
Xpath_code="/HTML/body/div/div [1]/div [2]/div/div [2]/div/div [2]/div [1]/div [4]/div [2]/table/tbody/tr/td [1]/[% d] div/a/span" % get_code
Stock_code=browser_code. Find_element_by_xpath (xpath_code). The text
List_stock_codes. Append (stock_code)
Time. Sleep (0.5)
Browser_code. Close ()

Browser_stock=webdriver. Chrome ()
For num in list_stock_codes:
Browser_stock. Get (" http://f10.eastmoney.com/f10_v2/FinanceAnalysis.aspx? Code="+ num)
Botton_year=browser_stock. Find_element_by_xpath ('/HTML/body/div [1]/div [2]/div [4]/div [3]/ul/li [2] '). Click ()
List_revenue=[]
For I in range (2, 7) : # traversal operating income of listed companies in 2015-2019
Try:
Xpath="/HTML/body/div [1]/div [2]/div [4]/div [4]/div/table/tbody tr [% d] [10]/td/span" % I
Var=browser_stock. Find_element_by_xpath (xpath). The text
List_revenue. Append (only_num. Search (var) group ())
Except:
The continue
Try:
stock_revenue=pd. DataFrame ({" stock code: "num,
"2019" : [list_revenue [0]],
"2018" : [list_revenue [1]], "2017" : [list_revenue [2]],
"2016" : [list_revenue [3]], "2015" : [list_revenue [4]]})
Stock_revenue. To_excel (writer, sheet_name="revenue")


Writer. The save ()
Except:
The continue
Browser_stock. Close ()

The red part of the cycle many times, and finally to write stock_info. The.xls files, important is can't cover the original content, roughly mean cycle once written, writing content in excel

CodePudding user response:



Look at the API have,,

The parameters of to_excel have

Startrowint, default 0
Startcolint, default 0

Look for is not suitable for

CodePudding user response:

Still can't write, don't understand

CodePudding user response:

Obtain historical data first, then append in, to write in

CodePudding user response:

To solve, thank you for your attention
  • Related