爬虫pandas存储报错raise ValueError("Cannot convert {0!r} to Excel".format(value))

爬虫pandas存储报错raise ValueError("Cannot convert {0!r} to Excel".format(value

  • 查找报错原因
  • 解决

查找报错原因

代码如下:

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")

报错如下

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

将blog_author变量类型打印一下
结果如下:

<class 'lxml.etree._ElementUnicodeResult'>

可以看到并不是传统的变量类型,所以可能错误在此

解决

我们尝试将blog_author 的变量类型转换为字符串
代码修改如下:

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

问题解决!!

你可能感兴趣的:(爬虫,python)