Python 爬取 baidu 股票市值資料

風中之楓發表於2019-02-16

Code:

```
Created on 2018年2月11日

python 3.6

@author: Livon

```

import urllib.request

import re

url = `https://gupiao.baidu.com/stock/sz002633.html`

print( `url: ` + url )

htmlResponse = urllib.request.urlopen( url )
html = htmlResponse.read()
html = html.decode(`utf8`)

marketCaps = re.findall("<dl><dt>總市值</dt><dd>(.*?)億</dd></dl>",html)
# 匹配換行
dates = re.findall(`<span class="state f-up">(.*?)s+</span>`,html)

for i in range( 0, len( marketCaps )):
    print( `總市值:` + marketCaps[i] + ` 億` )
    
for i in range( 0, len( dates )):
#     print(`時間:`, end=`` )    
    date = re.sub( "&nbsp;", "", dates[i] )
    print(`時間:` + date )
        

程式碼摘選

  • html = html.decode(`utf8`) # 轉碼
  • dates = re.findall(`<span class="state f-up">(.*?)s+</span>`,html) # 匹配換行
  • print(`時間:`, end=`` ) # 不換行
  • date = re.sub( "&nbsp;", "", dates[i] ) # 正則替換

output

url: https://gupiao.baidu.com/stock/sz002633.html
總市值:12.69 億
時間:已休市 2018-02-09 15:00:03

相關文章