日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫股票市盈率_使用python爬虫实现网络股票信息爬取的demo

發布時間:2023/12/8 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫股票市盈率_使用python爬虫实现网络股票信息爬取的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()

優化并且加入進度條顯示

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,fpath):

count = 0

for stock in lst:

url = stockURL + stock + ".html"

html = getHTMLText(url)

try:

if html == "":

continue

infoDict = {}

soup = BeautifulSoup(html,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,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,output_file)

main()

以上這篇使用python爬蟲實現網絡股票信息爬取的demo就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持編程小技巧。

總結

以上是生活随笔為你收集整理的python爬虫股票市盈率_使用python爬虫实现网络股票信息爬取的demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。