import requests from lxml import etree import csv 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' } f = open('4399益智副本2.csv', mode='w', encoding='utf-8-sig', newline='') csv_writer = csv.DictWriter(f, fieldnames=[ '游戏地址','游戏名称']) csv_writer.writeheader() for i in range(1,124): if i == 1: url = 'https://www.4399.com/flash_fl/5_1.htm' html = requests.get(url,headers=headers).content doc = etree.HTML(html) id = doc.xpath('//*[@id="classic"]/li/a/@href') name = doc.xpath('//*[@id="classic"]/li/a/text()') url_1 = 'https://www.4399.com'+id data_list = [] for i in range(len(name)): dit = {} dit['游戏地址'] = url_1[i] dit['游戏名称'] = name[i] data_list.append(dit) csv_writer.writerow(dit) else: url = 'https://www.4399.com/flash_fl/more_5_{}.htm'.format(str(i)) html = requests.get(url,headers=headers).content doc = etree.HTML(html) id = doc.xpath('//*[@id="classic"]/li/a/@href') name = doc.xpath('//*[@id="classic"]/li/a/text()') url_1 = 'https://www.4399.com'+id data_list = [] for i in range(len(name)): dit = {} dit['游戏地址'] = url_1[i] dit['游戏名称'] = name[i] data_list.append(dit) csv_writer.writerow(dit) print(data_list)这个摆错怎么改
时间: 2024-04-28 07:26:22 浏览: 157
from lxml import etree
你的代码中存在一个问题,即在第15行中,你将 `id` 变量赋值为一个列表,而不是单个链接。因此,你需要将其改为 `id[i]`:
```
url_1 = 'https://www.4399.com' + id[i]
```
另外,你也需要将 `csv_writer.writerow(dit)` 放在 for 循环外面,否则你在每次循环中都会写入一行,导致重复写入。
下面是修改后的代码:
```
import requests
from lxml import etree
import csv
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'
}
f = open('4399益智副本2.csv', mode='w', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['游戏地址', '游戏名称'])
csv_writer.writeheader()
for i in range(1, 124):
if i == 1:
url = 'https://www.4399.com/flash_fl/5_1.htm'
else:
url = 'https://www.4399.com/flash_fl/more_5_{}.htm'.format(str(i))
html = requests.get(url, headers=headers).content
doc = etree.HTML(html)
id = doc.xpath('//*[@id="classic"]/li/a/@href')
name = doc.xpath('//*[@id="classic"]/li/a/text()')
data_list = []
for i in range(len(name)):
dit = {}
dit['游戏地址'] = 'https://www.4399.com' + id[i]
dit['游戏名称'] = name[i]
data_list.append(dit)
csv_writer.writerows(data_list)
print(data_list)
f.close()
```
阅读全文