简体   繁体   中英

In python raise ValueError("Cannot convert {0!r} to Excel".format(value))

In python, I developed parsing program. My program recived data in website. I want to send Execl cell. but I received error message. I want to solve myself, but I can't I spent 7days this problem.

# -*- coding:utf-8 -*-

from __future__ import unicode_literals
from urllib.request import Request, urlopen
from openpyxl import load_workbook
from bs4 import BeautifulSoup
import pyautogui
pyautogui.FAILSAFE = False

    
    
    #엑셀 로그인
    
    
    load_owner_order_wb = load_workbook("order.xlsx", data_only=True)
    load_owner_order_ws = load_owner_order_wb['Sheet1']
    
    owner_source = load_owner_source_ws['A1'].value
    
    
    # html = urlopen(owner_source).read()
    html = urlopen('https://ownerclan.com/V2/product/view.php?selfcode=W13B289').read()
    
    soup = BeautifulSoup(html, "html.parser")
    
    i=1
    
    while len(load_owner_order_ws['A']) >= i:
        i = i + 1
    
    load_owner_order_ws['A%d'%i] = soup.title.string
    load_owner_order_ws['B%d'%i] = soup.find('span', class_='point_color_b2').string
    print(soup.select('#productPrice'))
    
    load_owner_order_ws['C%d'%i] = soup.select('#productPrice')
    load_owner_order_wb.save("order.xlsx")
Traceback (most recent call last):
  File "D:/python/smart_store/owner_clan.py", line 111, in <module>
    load_owner_order_ws['C%d'%i] = soup.select('#productPrice')
  File "D:\python\smart_store\venv\lib\site-packages\openpyxl\worksheet\worksheet.py", line 313, in __setitem__
    self[key].value = value
  File "D:\python\smart_store\venv\lib\site-packages\openpyxl\cell\cell.py", line 216, in value
    self._bind_value(value)
  File "D:\python\smart_store\venv\lib\site-packages\openpyxl\cell\cell.py", line 199, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert [<span id="productPrice">8,100</span>] to Excel

I have more experience with OpenPyXl than Beautiful Soup but the error seems to be telling you that soup.select('#productPrice') is returning [<span id="productPrice">8,100</span>] or basically a list of HTML elements (that happens to only have one value). If you want the innerHTML value of #productPrice then try soup.select('#productPrice')[0].text or soup.find('#productPrice').text .

I may be wrong about the .text part. Please refer to Beautiful Soup documentation to figure out how to extract the info you want from HTML!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM