Scrapy returns 结果正确到终端,但只将第一个结果写入 .CSV
Scrapy returns results correctly to terminal but only writes first result to .CSV
此蜘蛛抓取通过将唯一产品 ID 从 csv 文件添加到 URL 并从给定页面抓取数据而形成的串联链接。
使用-O命令将结果写入.CSV时,只写入第一个结果。
所有项目都被正确选择和循环,正确的结果显示在终端的输出中,但只有第一个项目被写入 CSV。
你不能在这样的循环中使用 if else 吗?
定义产量时,response.xpath 中的 x 还需要另一个吗?
写入路径和输出文件没有错误
我尝试了几种变体并创建了一个 for try 除了产生的每个元素,但结果是相同的
如果您有任何解决此问题的建议,请告诉我,谢谢
import scrapy
import pandas as pd
def readcsv():
df = pd.read_csv('tsuji2.csv')
#return df.values
return df['URL'].values.tolist()
class TsujijsonSpider(scrapy.Spider):
name = 'tsujijson22'
start_urls = ['https://www.example.co.jp/store/online/']
def parse(self, response):
for URL in readcsv():
base_url = 'https://www.example.co.jp/store/online/p/{}'
yield scrapy.Request(base_url.format(URL), callback=self.data)
def data(self, response):
try:
if response.xpath(u"//span[contains(text(), '〇')]").get():
yield{
'zaiko green' : response.css('span.green::text').get()}
elif response.xpath(u"//span[contains(text(), '△')]").get():
yield{
'zaiko red': response.css('span.red:nth-of-type(1)::text').get()}
else: yield {
'zaiko red2': response.css('span.upperRight::text').get()}
except:
pass
也许你必须在命令末尾键入 -t csv,例如:
scrapy crawl my_spider -o my_csv_file.csv -t csv
另外,我不知道它是否适合你,但我通常将我的结果写在一个 json line 文件中——每一行代表一个 json.
我在终端上输入:
scrapy crawl my_spider -o my_json_line_file.jl
有一点很重要,那就是你的蜘蛛所在的文件夹。例如,在 运行 我上面写的命令之前,我输入:
cd my_project_folder/my_project_folder/spiders
希望对你有所帮助! :)
更新
我尝试 运行 几乎相同的代码(仅更改 URL 列表来源)并且它有效。这是我使用的代码:
import scrapy
class MatsukiyoSpider(scrapy.Spider):
name = 'matsukiyo'
start_urls = ['https://www.matsukiyo.co.jp/store/online/']
def parse(self, response):
for URL in ["4582352470220", "4901061475737", "4901061475942"]:
base_url = 'https://www.matsukiyo.co.jp/store/online/p/{}'
yield scrapy.Request(base_url.format(URL), callback=self.data)
def data(self, response):
try:
if response.xpath(u"//span[contains(text(), '〇')]").get():
yield{
'zaiko green' : response.css('span.green::text').get()}
elif response.xpath(u"//span[contains(text(), '△')]").get():
yield{
'zaiko red': response.css('span.red:nth-of-type(1)::text').get()}
else: yield {
'zaiko red2': response.css('span.upperRight::text').get()}
except:
pass
这是我的 CSV 文件:
CSV result with only 3 URLs
PS:我在终端上输入了以下内容:
scrapy crawl matsukiyo -s LOG_LEVEL=DEBUG -o mats.csv -t csv
似乎所有结果都必须具有相同的列header名称才能正确写入 CSV
def data(self, response):
try:
if response.xpath(u"//span[contains(text(), '〇')]").get():
yield{
'zaiko' : response.css('span.green::text').get()}
elif response.xpath(u"//span[contains(text(), '△')]").get():
yield{
'zaiko': response.css('span.red:nth-of-type(1)::text').get()}
else: yield {
'zaiko': response.css('span.upperRight::text').get()}
except:
pass
将在一列中产生正确的结果
此蜘蛛抓取通过将唯一产品 ID 从 csv 文件添加到 URL 并从给定页面抓取数据而形成的串联链接。
使用-O命令将结果写入.CSV时,只写入第一个结果。
所有项目都被正确选择和循环,正确的结果显示在终端的输出中,但只有第一个项目被写入 CSV。
你不能在这样的循环中使用 if else 吗? 定义产量时,response.xpath 中的 x 还需要另一个吗?
写入路径和输出文件没有错误
我尝试了几种变体并创建了一个 for try 除了产生的每个元素,但结果是相同的
如果您有任何解决此问题的建议,请告诉我,谢谢
import scrapy
import pandas as pd
def readcsv():
df = pd.read_csv('tsuji2.csv')
#return df.values
return df['URL'].values.tolist()
class TsujijsonSpider(scrapy.Spider):
name = 'tsujijson22'
start_urls = ['https://www.example.co.jp/store/online/']
def parse(self, response):
for URL in readcsv():
base_url = 'https://www.example.co.jp/store/online/p/{}'
yield scrapy.Request(base_url.format(URL), callback=self.data)
def data(self, response):
try:
if response.xpath(u"//span[contains(text(), '〇')]").get():
yield{
'zaiko green' : response.css('span.green::text').get()}
elif response.xpath(u"//span[contains(text(), '△')]").get():
yield{
'zaiko red': response.css('span.red:nth-of-type(1)::text').get()}
else: yield {
'zaiko red2': response.css('span.upperRight::text').get()}
except:
pass
也许你必须在命令末尾键入 -t csv,例如:
scrapy crawl my_spider -o my_csv_file.csv -t csv
另外,我不知道它是否适合你,但我通常将我的结果写在一个 json line 文件中——每一行代表一个 json.
我在终端上输入:
scrapy crawl my_spider -o my_json_line_file.jl
有一点很重要,那就是你的蜘蛛所在的文件夹。例如,在 运行 我上面写的命令之前,我输入:
cd my_project_folder/my_project_folder/spiders
希望对你有所帮助! :)
更新
我尝试 运行 几乎相同的代码(仅更改 URL 列表来源)并且它有效。这是我使用的代码:
import scrapy
class MatsukiyoSpider(scrapy.Spider):
name = 'matsukiyo'
start_urls = ['https://www.matsukiyo.co.jp/store/online/']
def parse(self, response):
for URL in ["4582352470220", "4901061475737", "4901061475942"]:
base_url = 'https://www.matsukiyo.co.jp/store/online/p/{}'
yield scrapy.Request(base_url.format(URL), callback=self.data)
def data(self, response):
try:
if response.xpath(u"//span[contains(text(), '〇')]").get():
yield{
'zaiko green' : response.css('span.green::text').get()}
elif response.xpath(u"//span[contains(text(), '△')]").get():
yield{
'zaiko red': response.css('span.red:nth-of-type(1)::text').get()}
else: yield {
'zaiko red2': response.css('span.upperRight::text').get()}
except:
pass
这是我的 CSV 文件:
CSV result with only 3 URLs
PS:我在终端上输入了以下内容:
scrapy crawl matsukiyo -s LOG_LEVEL=DEBUG -o mats.csv -t csv
似乎所有结果都必须具有相同的列header名称才能正确写入 CSV
def data(self, response):
try:
if response.xpath(u"//span[contains(text(), '〇')]").get():
yield{
'zaiko' : response.css('span.green::text').get()}
elif response.xpath(u"//span[contains(text(), '△')]").get():
yield{
'zaiko': response.css('span.red:nth-of-type(1)::text').get()}
else: yield {
'zaiko': response.css('span.upperRight::text').get()}
except:
pass
将在一列中产生正确的结果