欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python爬蟲實(shí)現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo

 更新時(shí)間:2018年01月05日 14:12:00   作者:OliverkingLi  
下面小編就為大家分享一篇使用python爬蟲實(shí)現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

實(shí)例如下所示:

import requests
from bs4 import BeautifulSoup
import traceback
import re
 
def getHTMLText(url):
 try:
  r = requests.get(url)
  r.raise_for_status()
  r.encoding = r.apparent_encoding
  return r.text
 except:
  return ""
 
def getStockList(lst, stockURL):
 html = getHTMLText(stockURL)
 soup = BeautifulSoup(html, 'html.parser') 
 a = soup.find_all('a')
 for i in a:
  try:
   href = i.attrs['href']
   lst.append(re.findall(r"[s][hz]\d{6}", href)[0])
  except:
   continue
 
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='utf-8') as f:
    f.write( str(infoDict) + '\n' )
  except:
   traceback.print_exc()
   continue
 
def main():
 stock_list_url = 'http://quote.eastmoney.com/stocklist.html'
 stock_info_url = 'https://gupiao.baidu.com/stock/'
 output_file = 'D:/BaiduStockInfo.txt'
 slist=[]
 getStockList(slist, stock_list_url)
 getStockInfo(slist, stock_info_url, output_file)
 
main()

優(yōu)化并且加入進(jìn)度條顯示

import requests
from bs4 import BeautifulSoup
import traceback
import re
def getHTMLText(url, code="utf-8"):
 try:
  r = requests.get(url)
  r.raise_for_status()
  r.encoding = code
  return r.text
 except:
  return ""
def getStockList(lst, stockURL):
 html = getHTMLText(stockURL, "GB2312")
 soup = BeautifulSoup(html, 'html.parser')
 a = soup.find_all('a')
 for i in a:
  try:
   href = i.attrs['href']
   lst.append(re.findall(r"[s][hz]\d{6}", href)[0])
  except:
   continue
def getStockInfo(lst, stockURL, fpath):
 count = 0
 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='utf-8') as f:
    f.write(str(infoDict) + '\n')
    count = count + 1
    print("\r當(dāng)前進(jìn)度: {:.2f}%".format(count * 100 / len(lst)), end="")
  except:
   count = count + 1
   print("\r當(dāng)前進(jìn)度: {:.2f}%".format(count * 100 / len(lst)), end="")
   continue
def main():
 stock_list_url = 'http://quote.eastmoney.com/stocklist.html'
 stock_info_url = 'https://gupiao.baidu.com/stock/'
 output_file = 'BaiduStockInfo.txt'
 slist = []
 getStockList(slist, stock_list_url)
 getStockInfo(slist, stock_info_url, output_file)
main()

以上這篇使用python爬蟲實(shí)現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pytorch distributed 多卡并行載入模型操作

    Pytorch distributed 多卡并行載入模型操作

    這篇文章主要介紹了Pytorch distributed 多卡并行載入模型操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python 中將值附加到集合的操作方法

    Python 中將值附加到集合的操作方法

    這篇文章主要介紹了Python 中將值附加到集合的操作方法,通過使用 add() 方法或 update() 方法,你可以向 Python 中的集合中添加元素,在添加元素時(shí),需要注意不允許重復(fù)元素和集合是無序的,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Python3中的指針你了解嗎

    Python3中的指針你了解嗎

    Python這個(gè)編程語言雖然沒有指針類型,但是Python中的可變參量也可以像指針一樣,改變一個(gè)數(shù)值之后,所有指向該數(shù)值的可變參量都會(huì)隨之而改變,這篇文章主要介紹了Python3中的“指針”,需要的朋友可以參考下
    2024-02-02
  • pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼

    pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼

    本文主要介紹了pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-02-02
  • Python GUI自動(dòng)化實(shí)現(xiàn)繞過驗(yàn)證碼登錄

    Python GUI自動(dòng)化實(shí)現(xiàn)繞過驗(yàn)證碼登錄

    這篇文章主要介紹了python GUI自動(dòng)化實(shí)現(xiàn)繞過驗(yàn)證碼登錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Python深入學(xué)習(xí)之內(nèi)存管理

    Python深入學(xué)習(xí)之內(nèi)存管理

    這篇文章主要介紹了Python深入學(xué)習(xí)之內(nèi)存管理,本文比較詳細(xì)的講解了Python的內(nèi)存管理相關(guān)知識(shí),需要的朋友可以參考下
    2014-08-08
  • Python連接Oracle數(shù)據(jù)庫的操作指南

    Python連接Oracle數(shù)據(jù)庫的操作指南

    Oracle數(shù)據(jù)庫是一種強(qiáng)大的企業(yè)級(jí)關(guān)系數(shù)據(jù)庫管理系統(tǒng)(RDBMS),而Python是一門流行的編程語言,兩者的結(jié)合可以提供出色的數(shù)據(jù)管理和分析能力,本教程將詳細(xì)介紹如何在Python中連接Oracle數(shù)據(jù)庫,并演示常見的數(shù)據(jù)庫任務(wù),需要的朋友可以參考下
    2023-11-11
  • Django框架中處理URLconf中特定的URL的方法

    Django框架中處理URLconf中特定的URL的方法

    這篇文章主要介紹了Django框架中處理URLconf中特定的URL的方法,Django是豐富多彩的Python框架中最具人氣的一個(gè),需要的朋友可以參考下
    2015-07-07
  • 對(duì)python 判斷數(shù)字是否小于0的方法詳解

    對(duì)python 判斷數(shù)字是否小于0的方法詳解

    今天小編就為大家分享一篇對(duì)python 判斷數(shù)字是否小于0的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python numpy生成等差數(shù)列、等比數(shù)列的實(shí)例

    python numpy生成等差數(shù)列、等比數(shù)列的實(shí)例

    今天小編就為大家分享一篇python numpy生成等差數(shù)列、等比數(shù)列的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02

最新評(píng)論