html5 - python 處理html頁面爬蟲數(shù)據(jù)
問題描述
請求的url 數(shù)據(jù)http://www.hkex.com.hk/chi/st...對了我只抓取一張表,希望能夠提取關鍵表的數(shù)據(jù).
希望抓取的數(shù)據(jù)是該成交報表,但是HTML 的標簽都是<pre>造成了數(shù)據(jù)提取的困難。
賣空成交量 成交量
代號 股票名稱 股數(shù)(SH) 金額($)股數(shù)(SH) 金額($)
1 長和 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 九龍倉集團 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 電能實業(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 恒隆集團 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 會德豐 72,000 3,407,750 683,36832,286,993 23 東亞銀行 451,600 14,991,890 1,817,00060,295,348 27 銀河娛樂 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 遠東發(fā)展 10,00033,600 428,075 1,440,321 38 第一拖拉機股份 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')
應該如何提取該表格的數(shù)據(jù)內容。
問題解答
回答1:解決方法一:首先先定位賣空成交量位置 a = soup.find(’a’, attrs={’name’:’short_selling’}),然后根據(jù)pre->font的相鄰關系,一直往下走直到列不到6行就結束
這是結果:[[’代號’, ’股票名稱’, ’股數(shù)(SH)’, ’金額($)’, ’股數(shù)(SH)’, ’金額($)’], [’1’, ’長和’, ’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:
給你一個方案吧。
因為這些數(shù)據(jù)都是文本信息,沒有標簽包圍。通過抓包,也沒有發(fā)現(xiàn)特定的數(shù)據(jù)查詢接口。所以數(shù)據(jù)應該是服務器生成好的通過html寫死的發(fā)送給瀏覽器。那么發(fā)現(xiàn)這些數(shù)據(jù)項每一個特定的屬性都是占用同樣的位置大小且居右對齊,而且每一項有特定的格式,可以使用正則表達式進行提取。具體還是請您自行實現(xiàn)吧。回答3:
干嘛這么麻煩用beautifulsoup,殺雞焉用牛刀
你的網(wǎng)頁只有一行行數(shù)據(jù)啊,格式簡單的不能再簡單
你直接把頁面上的數(shù)據(jù)復制下來,保存成txt,然后用readline、split、正則表達式提取數(shù)據(jù)不就可以了嘛
相關文章:
1. nignx - docker內nginx 80端口被占用2. javascript - canvas 裁剪空白區(qū)域3. angular.js - angular內容過長展開收起效果4. docker不顯示端口映射呢?5. docker綁定了nginx端口 外部訪問不到6. 為什么我ping不通我的docker容器呢???7. docker網(wǎng)絡端口映射,沒有方便點的操作方法么?8. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?9. docker api 開發(fā)的端口怎么獲取?10. debian - docker依賴的aufs-tools源碼哪里可以找到啊?
