f = open('data.csv',mode = 'a',encoding = 'utf-8','newline = ') csv_writer = csv.DictWriter(f,fieldnames = [ '项目名称', '项目地址', '开发商名称', '开盘日期', ]) csv_writer.writeheader()
时间: 2023-08-06 09:20:48 浏览: 131
这段代码的作用是创建一个CSV文件并写入表头数据。具体来说,它做了以下几件事情:
1. 使用 open() 函数创建一个名为 'data.csv' 的文件对象 f,使用 'a' 模式打开文件以便在文件末尾添加新内容,使用 'utf-8' 编码以便处理中文字符,使用 'newline' 参数指定换行符为 '\n',以确保在不同操作系统上生成的文件格式一致。
2. 使用 csv.DictWriter() 函数创建一个 CSV 写入对象 csv_writer,并传入文件对象 f 和表头数据 fieldnames。
3. 使用 csv_writer.writeheader() 方法将表头数据写入文件。
相关问题
with open('data.csv', 'w', encoding='utf-8', newline='') as f: writer = csv.writer(f) writer.writerows(rows)
This code snippet opens a file named "data.csv" in write mode with UTF-8 encoding and no newlines. It then creates a CSV writer object and writes the list of rows to the file using the writerows() method. Each row in the list is written as a separate line in the CSV file, with each value separated by a comma. This code is useful for saving data in a CSV format that can be easily read by spreadsheet applications or other programs.
import requests import parsel import csv f = open('射击.csv', mode='w', encoding='utf-8-sig', newline='') csv_writer = csv.DictWriter(f, fieldnames=['游戏名字', '游戏地址']) csv_writer.writeheader() for page in range(1, 300): url = 'https://www.4399.com/flash_fl/4_1.htm'.format(page) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=url, headers=headers) response.encoding = response.apparent_encoding selector = parsel.Selector(response.text) lis = selector.css('#classic li') for li in lis: dit ={} data_url = li.css('a::attr(href)').get() new_url = 'http://www.4399.com' + data_url.replace('http://','/') dit['游戏地址'] = new_url title = li.css('img::attr(alt)').get() dit['游戏名字'] = title print(title, new_url) csv_writer.writerow(dit)怎么改成用xpath的
你可以将 `selector = parsel.Selector(response.text)` 改为 `selector = etree.HTML(response.text)`,然后使用 XPath 语法来解析 HTML 页面,下面是修改后的代码:
```
import requests
from lxml import etree
import csv
f = open('射击.csv', mode='w', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['游戏名字', '游戏地址'])
csv_writer.writeheader()
for page in range(1, 300):
url = 'https://www.4399.com/flash_fl/4_1.htm'.format(page)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
response.encoding = response.apparent_encoding
selector = etree.HTML(response.text)
lis = selector.xpath('//*[@id="classic"]/li')
for li in lis:
dit = {}
data_url = li.xpath('a/@href')[0]
new_url = 'http://www.4399.com' + data_url.replace('http://', '/')
dit['游戏地址'] = new_url
title = li.xpath('a/img/@alt')[0]
dit['游戏名字'] = title
print(title, new_url)
csv_writer.writerow(dit)
f.close()
```
这里使用 XPath 语法,将 `lis` 变量中的值直接改为 `li`,然后在内部使用 XPath 表达式来获取游戏地址和游戏名称。
阅读全文