擅长:python、mysql、java
<p>您可以使用正则表达式,仅搜索与预期模式匹配的内容:</p>
<p>如果你想找2.3或2:3之类的东西</p>
<p>用途(案例2.3):</p>
<pre><code>\d+(?:\.)+\d+
</code></pre>
<p>或使用(案例2:3):</p>
<pre><code>\d+(?:\:)+\d+
</code></pre>
<p>此代码将使用23:2的输入:</p>
<pre><code>import requests
import pandas as pd
import re
from bs4 import BeautifulSoup
record = []
hksi = ['CKH']
url = "http://www.etnet.com.hk/www/tc/futures/futures_stockoptions.php?atscode={}&month=202101"
for s in hksi:
response = requests.get(url.format(s))
info = response.text
soup = BeautifulSoup(info, "lxml")
bid = soup.find('td', {'style': 'padding:10px 0 5px 10px; border-top:1px dotted #e2e2e2; font-weight:bold;'}).text
ratio = soup.find('td', {'style': 'padding:10px 0 5px 0; border-top:1px dotted #e2e2e2; font-weight:bold;'}).text
ask = soup.find('td', {'style': 'padding:10px 10px 5px 0; border-top:1px dotted #e2e2e2; font-weight:bold;'}).text
record.append({
'symbol' : s,
'bid' : bid,
'ask' : ask,
'ratio': ratio
})
numbers = re.compile(r'\d+(?:\:)+\d+')
output = numbers.findall(bid)
print(output[0])
</code></pre>