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

Python獲取網(wǎng)頁(yè)數(shù)據(jù)詳解流程

 更新時(shí)間:2021年10月20日 15:34:18   作者:Mim.  
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python來(lái)獲取網(wǎng)頁(yè)的數(shù)據(jù),主要應(yīng)用了Requests庫(kù),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平

Requests 庫(kù)是 Python 中發(fā)起 HTTP 請(qǐng)求的庫(kù),使用非常方便簡(jiǎn)單。
發(fā)送 GET 請(qǐng)求
當(dāng)我們用瀏覽器打開(kāi)東旭藍(lán)天股票首頁(yè)時(shí),發(fā)送的最原始的請(qǐng)求就是 GET 請(qǐng)求,并傳入url參數(shù).

import requests
url='http://push2his.eastmoney.com/api/qt/stock/fflow/daykline/get'

用Python requests庫(kù)的get函數(shù)得到數(shù)據(jù)并設(shè)置requests的請(qǐng)求頭.

header={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}

得到network的參數(shù).

data={
    'cb': 'jQuery1123026726575651052076_1633873068863',
    'lmt': '0',
    'klt':' 101',
    'fields1': 'f1,f2,f3,f7',
    'fields2': 'f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63,f64,f65',
    'ut': 'b2884a393a59ad64002292a3e90d46a5',
    'secid': '0.000040',
    '_': '1633873068864'
}

我們使用 content 屬性來(lái)獲取網(wǎng)站返回的數(shù)據(jù),并命名為sd.

sd=requests.get(url=url,headers=header,data=data).content

json庫(kù)可以自字符串或文件中解析JSON。 該庫(kù)解析JSON后將其轉(zhuǎn)為Python字典或者列表。re模塊是python獨(dú)有的匹配字符串的模塊,該模塊中提供的很多功能是基于正則表達(dá)式實(shí)現(xiàn)的,而正則表達(dá)式是對(duì)字符串進(jìn)行模糊匹配,提取自己需要的字符串部分.

import json
import re
text=str(sd,'utf-8')
res=re.findall(r'[(](.*?)[)]',text)
re=json.loads(res[0])
p=re['data']['klines']

將雜亂無(wú)章的數(shù)據(jù)排版到excel中,代碼如下:

all_list=re['data']['klines']
data_list=[]
latest_price_list=[]
price_limit_list=[]
net_amount_list1=[]
net_proportion_list1=[]
net_amount_list2=[]
net_proportion_list2=[]
net_amount_list3=[]
net_proportion_list3=[]
net_amount_list4=[]
net_proportion_list4=[]
net_amount_list5=[]
net_proportion_list5=[]
for i in range(len(all_list)):
        data=all_list[i].split(',')[0]
        data_list.append(data)
        ##收盤(pán)價(jià)
        latest_price=all_list[i].split(',')[11]
        latest_price_list.append(latest_price)
        ##漲跌幅
        price_limit=all_list[i].split(',')[12]
        price_limit_list.append(price_limit)
        ##主力凈流入
        ####凈額
        net_amount1=all_list[i].split(',')[1]
        net_amount_list1.append(net_amount1)
        ##占比
        net_proportion1=all_list[i].split(',')[6]
        net_proportion_list1.append(net_proportion1)
        ##超大單凈流入
        ####凈額
        net_amount2=all_list[i].split(',')[5]
        net_amount_list2.append(net_amount2)
        ##占比
        net_proportion2=all_list[i].split(',')[10]
        net_proportion_list2.append(net_proportion2)
        ##大單凈流入
        ####凈額
        net_amount3=all_list[i].split(',')[4]
        net_amount_list3.append(net_amount3)
        ##占比
        net_proportion3=all_list[i].split(',')[9]
        net_proportion_list3.append(net_proportion3)
        ##中單凈流入
        ####凈額
        net_amount4=all_list[i].split(',')[3]
        net_amount_list4.append(net_amount4)
        ##占比
        net_proportion4=all_list[i].split(',')[8]
        net_proportion_list4.append(net_proportion4)
        ##小單凈流入
        ####凈額
        net_amount5=all_list[i].split(',')[2]
        net_amount_list5.append(net_amount5)
        ##占比
        net_proportion5=all_list[i].split(',')[7]
        net_proportion_list5.append(net_proportion5)
#print(data_list)
import pandas as pd
df=pd.DataFrame()
df['日期'] = data_list
df['收盤(pán)價(jià)'] = latest_price_list
df['漲跌幅(%)'] = price_limit_list
df['主力凈流入-凈額'] = net_amount_list1
df['主力凈流入-凈占比(%)'] = net_proportion_list1
df['超大單凈流入-凈額'] = net_amount_list2
df['超大單凈流入-凈占比(%)'] = net_proportion_list2
df['大單凈流入-凈額'] = net_amount_list3
df['大單凈流入-凈占比(%)'] = net_proportion_list3
df['中單凈流入-凈額'] = net_amount_list4
df['中單凈流入-凈占比(%)'] = net_proportion_list4
df['小單凈流入-凈額'] = net_amount_list5
df['小單凈流入-凈占比(%)'] = net_proportion_list5
df# 寫(xiě)入excel
df.to_excel('東旭藍(lán)天資金流向一覽表.xlsx')

將爬取出的東旭藍(lán)天資金流向數(shù)據(jù)存到excel表中,得到表格的部分截圖如下:

到此這篇關(guān)于Python獲取網(wǎng)頁(yè)數(shù)據(jù)詳解流程的文章就介紹到這了,更多相關(guān)Python 獲取網(wǎng)頁(yè)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論