import re
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import pandas as pd
import numpy as np
import time
only_num = re.compile('(\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,16):
xpath_code="/html/body/div/div[1]/div[2]/div/div[2]/div/div[2]/div[1]/div[4]/div[2]/table/tbody/tr[%d]/td[1]/div/a/span" %get_code
stock_code=browser_code.find_element_by_xpath(xpath_code).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): # 遍歷2015年-2019年上市公司營業收入
try:
xpath = "/html/body/div[1]/div[2]/div[4]/div[4]/div/table/tbody/tr[10]/td[%d]/span" %i
var = browser_stock.find_element_by_xpath(xpath).text
list_revenue.append(only_num.search(var).group())
except:
continue
try:
stock_revenue = pd.DataFrame({"股票代碼": 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.save()
except:
continue
browser_stock.close()
標紅的部分要回圈很多次,最后都要寫入stock_info.xls檔案,重要的是不能覆寫原有的內容,大致意思是沒回圈一次寫入,將內容寫入excel
uj5u.com熱心網友回復:

看看api就有,,
to_excel的引數有
startrowint, default 0
startcolint, default 0
看看適合不適合
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/267784.html
上一篇:求教 為何用rstudio讀取txt的文本資料以后結果顯示是這樣
下一篇:機器翻譯資料集的問題
