请求的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,323 64,672,175
10 恒隆集團 172,000 5,209,850 967,980 29,308,292
11 恒生銀行 189,000 30,047,370 1,075,185 170,873,130
12 恒基地產 94,000 4,025,500 1,382,533 59,183,598
14 希慎興業 33,000 1,167,900 642,424 22,747,393
16 新鴻基地產 425,000 45,490,800 1,635,959 175,284,039
17 新世界發展 651,000 5,833,670 10,135,381 90,633,244
19 太古股份公司A 132,000 10,405,600 554,962 43,709,235
20 會德豐 72,000 3,407,750 683,368 32,286,993
23 東亞銀行 451,600 14,991,890 1,817,000 60,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,000 33,600 428,075 1,440,321
38 第一拖拉機股份 8,000 38,200 1,634,000 7,825,940
41 鷹君 12,000 422,400 470,146 16,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")
应该如何提取该表格的数据内容。
解決方法一:
先定位賣空成交量位置a = soup.find('a', attrs={'name':'short_selling'}),然後根據pre->font的相鄰關係,一直往下走直到列不到6行就結束
這是結果:
原始碼
給你一個方案。
幹嘛這麼麻煩用
beautifulsoup
,殺雞焉用牛刀你的網頁只有一行行資料啊,格式簡單的不能再簡單
你直接把頁面上的資料複製下來,保存成
txt
,然后用readline
、split
、正規表達式提取資料不就可以了嘛