擅长:python、mysql、java
<p>在指定的URL上查找价格效果很好,只是忘记指定要在哪些工作表/单元格中写入数据。这样做:</p>
<pre><code>wb = Workbook()
# select sheet
ws = wb.active
i = 0
while i<len(symbolslist):
url = "http://finance.yahoo.com/q?s="+symbolslist[i]+"&q1=1"
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_'+symbolslist[i]+'">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print "The price of", symbolslist[i], " is ", price
# specify in which cell you want to write the price
# in this case: A1 to A4, where the row is specified by the index i
# rownumber must start at index 1
ws.cell(row=i+1, column=1).value = price[0]
i += 1
</code></pre>
<p>最后(小心它只会覆盖现有数据):</p>
^{pr2}$
<p>您还可以将其与下面的<a href="https://openpyxl.readthedocs.io/en/stable/" rel="nofollow noreferrer">openpyxl documentation</a>进行比较</p>
<p>如果您想要更多关于openpyxl模块的信息,可以打开python控制台:</p>
<pre><code>>>> import openpyxl
>>> help(openpyxl)
</code></pre>
<p>或者对于特定的包装内容:</p>
<pre><code>>>> help(openpyxl.workbook)
</code></pre>