我编写了一个脚本,该脚本将四个不同站点的刮擦标题写入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)
带有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)