I am scraping a price from a website using lxml and I'd like to insert that price into an existing excel file using openpyxl. When I run the code, I get the error "ValueError: Cannot convert ['$364'] to Excel" ('$364 is the scrapped price). How do I fix this? It appears that line 11 of code: sheet['A1'] = price
is the problem. My enitre code is below.
from lxml import html
import requests
page = requests.get('http://www.randomlengths.com/Woodwire/RL-Lbr-Pnl/')
tree = html.fromstring(page.content)
price = tree.xpath('//*[@id="main-frame"]/div/div[1]/table/tbody/tr[2]/td[2]/strong/text()')
print(price)
import openpyxl
xfile = openpyxl.load_workbook('C:/Users/noah.merkousko/randomlengthslumber.xlsx')
sheet = xfile.get_sheet_by_name('Framing Lumber')
sheet['A1'] = price
xfile.save('random lengths lumber test.xls')
"ValueError: Cannot convert ['$364'] to Excel" is telling you the error. You are trying to place a list ['$364']
into a cell that contains values. You can fix this by either indexing when you put it in Excel or indexing when it's parsed from online.
Option 1:
price = tree.xpath('//*[@id="main-frame"]/div/div[1]/table/tbody/tr[2]/td[2]/strong/text()')[0] # index at 0
Option 2:
sheet['A1'] = price[0] # index when you put into Excel
It is worth noting that this may introduce an IndexValue
error if the site is altered and the value is no longer located at '//*[@id="main-frame"]/div/div[1]/table/tbody/tr[2]/td[2]/strong/text()'
, but otherwise should solve your problem
You can catch the error as an exception.
try:
sheet['A1'] = price
except ValueError:
print("Handling the error case")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.