亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

實例如下所示:

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)化并且加入進度條顯示

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當前進度: {:.2f}%".format(count * 100 / len(lst)), end="")
  except:
   count = count + 1
   print("\r當前進度: {:.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爬蟲實現(xiàn)網(wǎng)絡股票信息爬取的demo就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

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

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

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

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

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

    Python3中的指針你了解嗎

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

    pyqt6實現(xiàn)關閉窗口前彈出確認框的示例代碼

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

    Python GUI自動化實現(xiàn)繞過驗證碼登錄

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

    Python深入學習之內(nèi)存管理

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

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

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

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

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

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

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

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

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

最新評論