Python&;Excel连接
如何连接第27行?我得到“TypeError:无法连接'str'和'list'对象”。这是因为我在符号列表中使用了一个变化的变量吗 这就是我所说的一句话:Python&;Excel连接,python,excel,concatenation,Python,Excel,Concatenation,如何连接第27行?我得到“TypeError:无法连接'str'和'list'对象”。这是因为我在符号列表中使用了一个变化的变量吗 这就是我所说的一句话: ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + price 下面是我的代码和第27行的重写 from openpyxl import Workbook import urllib import re from openpyxl.cell import
ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + price
下面是我的代码和第27行的重写
from openpyxl import Workbook
import urllib
import re
from openpyxl.cell import get_column_letter
wb = Workbook()
dest_filename = r'empty_book.xlsx'
ws = wb.create_sheet()
ws.title = 'Stocks'
symbolslist = ["aapl","spy","goog","nflx"]
i=0
while i<len(symbolslist):
#counts each object as 1 in the list
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
ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + price
i+=1
wb.save(filename = dest_filename)
来自openpyxl导入工作簿的
导入URL库
进口稀土
从openpyxl.cell导入获取列字母
wb=工作簿()
dest_filename=r'empty_book.xlsx'
ws=wb.create_sheet()
ws.title=‘股票’
符号列表=[“aapl”、“spy”、“goog”、“nflx”]
i=0
而ire.findall()
总是返回一个列表;也许你想把它变成一个字符串:
ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + ','.join(price)
或者,仅打印第一个元素:
ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + price[0]
使用HTML解析器解析HTML更容易;将是一个更好的工具:
import urllib
from bs4 import BeautifulSoup
for symbol in symbolslist:
url = "http://finance.yahoo.com/q?s={}&q1=1".format(symbol)
response = urllib.urlopen(url)
soup = BeautifulSoup(response, from_encoding=response.headers.getparam('charset'))
price = soup.find('span', id='yfs_l84_' + symbol).text
text = "The price of {} is {}".format(symbol, price)
print text
ws.cell(1,i+1).value = text
快速演示:
>>> import urllib2
>>> from bs4 import BeautifulSoup
>>> symbolslist = ["aapl","spy","goog","nflx"]
>>> for symbol in symbolslist:
... url = "http://finance.yahoo.com/q?s={}&q1=1".format(symbol)
... response = urllib.urlopen(url)
... soup = BeautifulSoup(response, from_encoding=response.headers.getparam('charset'))
... price = soup.find('span', id='yfs_l84_' + symbol).text
... text = "The price of {} is {}".format(symbol, price)
... print text
...
The price of aapl is 483.03
The price of spy is 168.89
The price of goog is 872.35
The price of nflx is 327.26
re.findall()
始终返回列表;也许你想把它变成一个字符串:
ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + ','.join(price)
或者,仅打印第一个元素:
ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + price[0]
使用HTML解析器解析HTML更容易;将是一个更好的工具:
import urllib
from bs4 import BeautifulSoup
for symbol in symbolslist:
url = "http://finance.yahoo.com/q?s={}&q1=1".format(symbol)
response = urllib.urlopen(url)
soup = BeautifulSoup(response, from_encoding=response.headers.getparam('charset'))
price = soup.find('span', id='yfs_l84_' + symbol).text
text = "The price of {} is {}".format(symbol, price)
print text
ws.cell(1,i+1).value = text
快速演示:
>>> import urllib2
>>> from bs4 import BeautifulSoup
>>> symbolslist = ["aapl","spy","goog","nflx"]
>>> for symbol in symbolslist:
... url = "http://finance.yahoo.com/q?s={}&q1=1".format(symbol)
... response = urllib.urlopen(url)
... soup = BeautifulSoup(response, from_encoding=response.headers.getparam('charset'))
... price = soup.find('span', id='yfs_l84_' + symbol).text
... text = "The price of {} is {}".format(symbol, price)
... print text
...
The price of aapl is 483.03
The price of spy is 168.89
The price of goog is 872.35
The price of nflx is 327.26
re.findall()
返回一个列表。它抱怨的是price
,而不是symbolslist
re.findall()
返回一个列表。它在抱怨price
,而不是symbolslist
。如何将re.findall()更改为字符串?@RobB.:通过获取第一个元素或将所有元素合并为一个字符串。我已经给出了这两个选项的示例。如何将re.findall()更改为字符串?@RobB.:通过获取第一个元素或将所有元素合并为字符串。我已经给了你两种选择的例子。