一、实现目标
本文利用股城网(https://hq.gucheng.com/gpdmylb.html)提供的股票代码数据,经由同花顺网(http://stockpage.10jqka.com.cn/000001/ )检索交易结束后的当日开盘价格、收盘价格。
其中,股城网的网页数据(目标:股票代码、股票名称)通过BeautifulSoup抓取,同花顺网的目标数据(交易结束后的当日开盘价格、收盘价格)转化成json字典后提取,最后录入EXCEL文件。
二、使用步骤
1.引入库
import requests
import json
import bs4
from bs4 import BeautifulSoup
import traceback
import re
import openpyxl
from datetime import datetime
2.读入数据
def getHTMLText(url):
try:
headers={'user-agent':'Mozilla/5.0'}
r = requests.get(url, headers=headers,timeout = 30)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.status_code)
return r.text
except:
return ""
def getStockList(infoDict,stock_code_url):