我编写了一个脚本,它将四个不同站点的搜索标题写入一张csv文件。是否有可能如果我想在一个csv文件中在四个不同的工作表中编写四个不同的标题?这是我到目前为止所尝试的:
import csv
import requests
from lxml import html
web_list = ['www.dailynews.com','www.dailynews.co.zw','www.gulf-daily-news.com','www.dailynews.gov.bw']
outfile=open("title.csv","w",newline='')
writer=csv.writer(outfile)
for websites in web_list:
url = "http://" + websites
page = requests.get(url).text
tree= html.fromstring(page)
for site_title in tree.xpath("//title"):
title=site_title.xpath(".//text()")
writer.writerow(title)
答案 0 :(得分:1)
使用python3(更容易处理unicode)和lib提到你可以转换你的代码,如:
import requests
from lxml import html
from pyexcel_ods3 import save_data
web_list = ['www.dailynews.com','www.dailynews.co.zw','www.gulf-daily-news.com','www.dailynews.gov.bw']
outfile=open("title.csv","w",newline='')
data = {}
for i, websites in enumerate(web_list):
url = "http://" + websites
page = requests.get(url).text
tree= html.fromstring(page)
for site_title in tree.xpath("//title"):
title=site_title.xpath(".//text()")
title.remove('\n')
data.update({"Sheet"+str(i): [[str(title[0])]]})
save_data("your_file.ods", data)