gpt4 book ai didi

python - 我正在尝试将 scrapy 结果输出到 csv 文件,但我的输出始终为空白

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:15 24 4
gpt4 key购买 nike

这是我的主蜘蛛 Bathuni.py

from scrapy.spider import Spider
from scrapy.selector import Selector
from bathUni.items import BathuniItem

class bathuni(Spider):
name = "bathU"
allowed_domains = ["http://international.southwales.ac.uk/"]
start_urls= ["http://international.southwales.ac.uk/country/argentina/en/",
"http://international.southwales.ac.uk/country/france/en/",
"http://international.southwales.ac.uk/country/australia/en/"]

def parse(self, response):
sel = Selector(response)
sites = sel.xpath('//div[@class="factsheet"]/ul/li')
for site in sites:
country = site.xpath('text()').extract()
return country

我的items.py如下

from scrapy.item import Item, Field

class BathuniItem(Item):
Country = Field()

我用来输出到 csv 文件的命令是

scrapy crawl bathU -o countries.csv -t csv

我的输出文件始终为空。任何帮助,将不胜感激。谢谢。

最佳答案

返回国家/地区更改为yield BathuniItem(Country=country)

这解决了两个问题:

  1. 错误:Spider 必须返回 Request、BaseItem 或 None,得到“unicode”
  2. 使用yield返回Python生成器 - 来自函数的多个项目

关于python - 我正在尝试将 scrapy 结果输出到 csv 文件,但我的输出始终为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com