首页 > 解决方案 > start_request 的循环槽 CSV

问题描述

我是 Python 新手,并试图遍历一个包含两列的 CSV 文件;姓名和邮政编码:

Name of Company, 1234

最终 URL 应如下所示:

https://www.example.com/result.aspx?what=Name+of+Comapny&where=1234

我试过这样:

类 csvSpider(scrapy.Spider): 名称 = 'csv_spider'

def start_requests(self):
    with open('test.csv') as f:
        for line in f:
            if not line.strip():
                continue
            print 'https://www.example.com/result.aspx?what=' + line[0] + '&where=' + line[1]   
            yield Request('https://www.example.com/result.aspx?what=' + line[0] + '&where=' + line[1])

我还在 Python 文件的开头导入了 CSV 模块。

不知道我做错了什么 - 我怎样才能做到这一点?

提前致谢!

标签: pythonscrapy

解决方案


推荐阅读