html5 - python 處理html頁面爬蟲數據
問題描述
請求的url 數據http://www.hkex.com.hk/chi/st...對了我只抓取一張表,希望能夠提取關鍵表的數據.
希望抓取的數據是該成交報表,但是HTML 的標簽都是<pre>造成了數據提取的困難。
賣空成交量 成交量
代號 股票名稱 股數(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 電能實業 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 恒基地產 94,000 4,025,500 1,382,53359,183,598 14 希慎興業 33,000 1,167,900 642,42422,747,393 16 新鴻基地產 425,000 45,490,800 1,635,959 175,284,039 17 新世界發展 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 九龍建業 31,000 228,260 292,000 2,156,291 35 遠東發展 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')
應該如何提取該表格的數據內容。
問題解答
回答1:解決方法一:首先先定位賣空成交量位置 a = soup.find(’a’, attrs={’name’:’short_selling’}),然后根據pre->font的相鄰關系,一直往下走直到列不到6行就結束
這是結果:[[’代號’, ’股票名稱’, ’股數(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:
給你一個方案吧。
因為這些數據都是文本信息,沒有標簽包圍。通過抓包,也沒有發現特定的數據查詢接口。所以數據應該是服務器生成好的通過html寫死的發送給瀏覽器。那么發現這些數據項每一個特定的屬性都是占用同樣的位置大小且居右對齊,而且每一項有特定的格式,可以使用正則表達式進行提取。具體還是請您自行實現吧?;卮?:
干嘛這么麻煩用beautifulsoup,殺雞焉用牛刀
你的網頁只有一行行數據啊,格式簡單的不能再簡單
你直接把頁面上的數據復制下來,保存成txt,然后用readline、split、正則表達式提取數據不就可以了嘛
相關文章:
1. mysql - 新浪微博中的關注功能是如何設計表結構的?3. angular.js使用$resource服務把數據存入mongodb的問題。4. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?5. angular.js - 關于$apply()6. dockerfile - [docker build image失敗- npm install]7. MySQL數據庫中文亂碼的原因8. 表單提交驗證,沒反應,求老師指點9. nignx - docker內nginx 80端口被占用10. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失?????
