【发布时间】:2018-09-21 21:13:29
【问题描述】:
我正在尝试编写一个 python 脚本,该脚本将来自网站的文本并将其放入 excel 中。我可以请求数据,但是将其转换为 excel 给我带来了一些困难。
from lxml import html
import requests
import xlsxwriter
import datetime
now = datetime.datetime.today().strftime('%Y-%m-%d')
page = requests.get('http://econpy.pythonanywhere.com/ex/001.html')
tree = html.fromstring(page.content)
#This will create a list of buyers
buyers = tree.xpath('//div[@title="buyer-name"]/text()')
#This will create a list of prices
prices = tree.xpath('//span[@class="item-price"]/text()')
print( 'Buyers: ', buyers)
print( 'Prices: ', prices)
expenses = (buyers, prices)
#creating excel sheet
workbook = xlsxwriter.Workbook('sales' + str(now) + '.xlsx')
worksheet = workbook.add_worksheet()
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
#write data to excel
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
workbook.close()
返回:回溯(最后一次调用): 文件“wRequests.py”,第 32 行,在 对于项目,成本(费用): ValueError:要解压的值太多(预期为 2)
如何解压这些值并正确加载到excel中?
【问题讨论】: