将上述过程封装成完成的函数,代码如下:
- def getStockInfo(lst, stockURL, fpath):
- for stock in lst:
- url = stockURL + stock + ".html"
- html = getHTMLText(url)
- try:
- if html=="":
- continue
- infoDict = {}
- soup = BeautifulSoup(html, 'html.parser')
- stockInfo = soup.find('div',attrs={'class':'stock-bets'})
- name = stockInfo.find_all(attrs={'class':'bets-name'})[0]
- infoDict.update({'股票名称': name.text.split()[0]})
- keyList = stockInfo.find_all('dt')
- valueList = stockInfo.find_all('dd')
- for i in range(len(keyList)):
- key = keyList[i].text
- val = valueList[i].text
- infoDict[key] = val
- with open(fpath, 'a', encoding=
推荐阅读
接口是面向对象JavaScript法度榜样员的对象箱中最有效的对象之一。在设计模式中提出的可重用的面向对象设计的原则之一就是“针对接口编程而不是实现编程”,即我们所说的面向接口编程,这个>>>详细阅读
本文标题:Python爬虫实战:股票数据定向爬虫
地址:http://www.17bianji.com/lsqh/36863.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示