html5 - python 處理html頁(yè)面爬蟲(chóng)數(shù)據(jù)
問(wèn)題描述
請(qǐng)求的url 數(shù)據(jù)http://www.hkex.com.hk/chi/st...對(duì)了我只抓取一張表,希望能夠提取關(guān)鍵表的數(shù)據(jù).
希望抓取的數(shù)據(jù)是該成交報(bào)表,但是HTML 的標(biāo)簽都是<pre>造成了數(shù)據(jù)提取的困難。
賣空成交量 成交量
代號(hào) 股票名稱 股數(shù)(SH) 金額($)股數(shù)(SH) 金額($)
1 長(zhǎng)和 299,500 27,572,475 2,201,171 202,964,029 2 中電控股 61,000 4,622,825 1,452,853 110,040,699 3 香港中華煤氣 2,939,000 42,694,880 8,024,558 116,691,466 4 九龍倉(cāng)集團(tuán) 297,000 17,349,550 3,136,238 183,105,286 5 匯豐控股 1,102,800 73,202,940 8,630,868 572,622,103 6 電能實(shí)業(yè) 1,016,500 76,262,725 4,876,990 365,926,231 8 電訊盈科 731,000 3,478,240 13,579,32364,672,175 10 恒隆集團(tuán) 172,000 5,209,850 967,98029,308,292 11 恒生銀行 189,000 30,047,370 1,075,185 170,873,130 12 恒基地產(chǎn) 94,000 4,025,500 1,382,53359,183,598 14 希慎興業(yè) 33,000 1,167,900 642,42422,747,393 16 新鴻基地產(chǎn) 425,000 45,490,800 1,635,959 175,284,039 17 新世界發(fā)展 651,000 5,833,670 10,135,38190,633,244 19 太古股份公司A 132,000 10,405,600 554,96243,709,235 20 會(huì)德豐 72,000 3,407,750 683,36832,286,993 23 東亞銀行 451,600 14,991,890 1,817,00060,295,348 27 銀河娛樂(lè) 1,134,000 40,408,550 15,089,117 538,712,668 31 航天控股 210,000 211,580 4,367,526 4,386,198 34 九龍建業(yè) 31,000 228,260 292,000 2,156,291 35 遠(yuǎn)東發(fā)展 10,00033,600 428,075 1,440,321 38 第一拖拉機(jī)股份 8,00038,200 1,634,000 7,825,940 41 鷹君 12,000 422,400 470,14616,546,562 45 大酒店 35,500 305,605 503,559 4,335,522
url = 'http://www.hkex.com.hk/chi/stat/smstat/dayquot/d20170202c.htm' response = requests.get(url) if response.status_code == 200:soup = BeautifulSoup(response.content, 'lxml')
應(yīng)該如何提取該表格的數(shù)據(jù)內(nèi)容。
問(wèn)題解答
回答1:解決方法一:首先先定位賣空成交量位置 a = soup.find(’a’, attrs={’name’:’short_selling’}),然后根據(jù)pre->font的相鄰關(guān)系,一直往下走直到列不到6行就結(jié)束
這是結(jié)果:[[’代號(hào)’, ’股票名稱’, ’股數(shù)(SH)’, ’金額($)’, ’股數(shù)(SH)’, ’金額($)’], [’1’, ’長(zhǎng)和’, ’299,500’, ’27,572,475’, ’2,201,171’, ’202,964,029’], [’2’, ’中電控股’, ’61,000’, ’4,622,825’, ’1,452,853’, ’110,040,699’], [’3’, ’香港中華煤氣’, ’2,939,000’, ’42,694,880’, ’8,024,558’, ’116,691,466’],....源代碼
import pprintfrom bs4 import BeautifulSoupimport requestsr = requests.get(’http://www.hkex.com.hk/chi/stat/smstat/dayquot/d170202c.htm’)r.encoding = ’big5’soup = BeautifulSoup(r.text)a = soup.find(’a’, attrs={’name’:’short_selling’})data = []pre = a.find_parent(’pre’)for line in pre.font.text.splitlines(): item = line.strip().split() if len(item) == 6:data.append(item)end = Falsefor next_pre in pre.next_siblings: for line in next_pre.font.text.splitlines():item = line.strip().split()if len(item) > 7: item = item[1:2] + [''.join(item[1:-4])] + item[-4:]elif len(item) < 6: end = True breakdata.append(item) if end: breakpprint.pprint(data)回答2:
給你一個(gè)方案吧。
因?yàn)檫@些數(shù)據(jù)都是文本信息,沒(méi)有標(biāo)簽包圍。通過(guò)抓包,也沒(méi)有發(fā)現(xiàn)特定的數(shù)據(jù)查詢接口。所以數(shù)據(jù)應(yīng)該是服務(wù)器生成好的通過(guò)html寫死的發(fā)送給瀏覽器。那么發(fā)現(xiàn)這些數(shù)據(jù)項(xiàng)每一個(gè)特定的屬性都是占用同樣的位置大小且居右對(duì)齊,而且每一項(xiàng)有特定的格式,可以使用正則表達(dá)式進(jìn)行提取。具體還是請(qǐng)您自行實(shí)現(xiàn)吧。回答3:
干嘛這么麻煩用beautifulsoup,殺雞焉用牛刀
你的網(wǎng)頁(yè)只有一行行數(shù)據(jù)啊,格式簡(jiǎn)單的不能再簡(jiǎn)單
你直接把頁(yè)面上的數(shù)據(jù)復(fù)制下來(lái),保存成txt,然后用readline、split、正則表達(dá)式提取數(shù)據(jù)不就可以了嘛
相關(guān)文章:
1. javascript - node.js promise沒(méi)用2. android 如何實(shí)現(xiàn)如圖中的鍵盤上的公式及edittext的內(nèi)容展示呢3. c++ - 如何正確的使用QWebEngineView?4. yii2中restful配置好后在nginx下報(bào)404錯(cuò)誤5. javascript - js 寫一個(gè)正則 提取文本中的數(shù)據(jù)6. 算法 - python 給定一個(gè)正整數(shù)a和一個(gè)包含任意個(gè)正整數(shù)的 列表 b,求所有<=a 的加法組合7. golang - 用IDE看docker源碼時(shí)的小問(wèn)題8. ruby - gitlab托管,git clone 失敗?9. java - 我在用Struts2上傳文件時(shí),報(bào)以下錯(cuò)誤怎么回事?10. 網(wǎng)站被黑,請(qǐng)教下大神,怎么對(duì)datebase.php內(nèi)容加密。
