Crawler pandas storage error value raise ("Cannot convert {0! R} to Excel" .format (value))

Crawler pandas storage error raise ValueError ("Cannot convert {0! R} to Excel" .format (value

Find the cause of the error

code show as below:

result = []
response = requests.get(url=url,headers = header)
print(response.text)
tree = html.fromstring(response.text)
blog_list = tree.xpath("//div[@class='card-wrap']")
for blog in blog_list:
	blog_author = blog.xpath("div[@class = 'card']//div/div[2]/p/@nick-name")[0]
	result.append(blog_author)
columns = ["作者"]
output = pd.DataFrame(result,columns=columns)
output.head()
output.to_excel("E:/HIT/unilever/try.xlsx")

The error is as follows

Traceback (most recent call last):
  File "E:/machineLearing/bolg.py", line 68, in <module>
    output.to_excel("E:/HIT/unilever/try.xlsx")
  File "D:\python\lib\site-packages\pandas\core\frame.py", line 1766, in to_excel
    engine=engine)
  File "D:\python\lib\site-packages\pandas\io\formats\excel.py", line 652, in write
    freeze_panes=freeze_panes)
  File "D:\python\lib\site-packages\pandas\io\excel.py", line 1395, in write_cells
    xcell.value, fmt = self._value_with_fmt(cell.val)
  File "D:\python\lib\site-packages\openpyxl\cell\cell.py", line 252, in value
    self._bind_value(value)
  File "D:\python\lib\site-packages\openpyxl\cell\cell.py", line 218, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert '木方格' to Excel

Print the blog_author variable type and the
result is as follows:

<class 'lxml.etree._ElementUnicodeResult'>

It can be seen that it is not a traditional variable type, so it may be wrong here

solve

We try to convert the variable type of blog_author to a string. The
code is modified as follows:

blog_author = str(blog.xpath("div[@class = 'card']//div/div[2]/p/@nick-name")[0])

problem solved! !

Published 7 original articles · won 11 · views 259

Guess you like

Origin blog.csdn.net/weixin_43165512/article/details/105340519