【问题标题】:How to make scrapy spider get start URL and allowed domains from csv list?如何让爬虫从 csv 列表中获取起始 URL 和允许的域?
【发布时间】:2021-02-19 03:21:09
【问题描述】:

我正在为我的研究项目使用爬虫爬虫进行 URL 抓取。我的蜘蛛基于来自 bhattraideb (Scrapy follow all the links and get status) 的代码,并稍作编辑以更好地满足我的需求。

目前,每次更改允许的域并启动 URL 时,我都会重新启动蜘蛛,因为我需要将每个允许域的输出放在单独的文件中。由于我的 URL 列表越来越多,这变得非常乏味......

我尝试迭代 .csv,使用“i”和“while”将两个列与 allowed_domains 和 start_urls 作为列表导入,但它总是与类发生冲突。
我会很感激任何帮助:-)

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    见:How to loop through multiple URLs to scrape from a CSV file in Scrapy?

    关于信息:当不使用 CSV 时,您也可以这样做,从列表中读取 start_urls

    # https://www.food.com/recipe/all/healthy?pn=1
    list_url = 'https://www.food.com/recipe/all/healthy?pn='
    start_urls = [list_url + str(page)]
    

    增加下一页的page变量,直到下一页为None。

    【讨论】:

    • 谢谢!我确实尝试了对我不起作用的公认答案,但是第二个答案。对 allowed_domains 使用相同的方法也很有效(蜘蛛会跟随链接,所以对我来说必须拥有某种黑名单)。关于如何为每个基本网址制作输出文件的任何想法?就像从 URL 抓取的所有 URL,例如food.com/recipe/all 被写入 www.food.com.txt?
    • 自己搞定:name = re.search(r"\/\/.*?\/", str(response.url)) nameclean = name.group() nameclean2 = nameclean.replace("/", "") filename = nameclean2 + ".txt" with open(filename, 'a') as f: f.write('\n'+str(response.url)+'\n')
    • 啊,是的,或者....如果你想尝试输出到 SQL 数据库,你可以放入一些条件逻辑来写入一个表(或多个/不同的表)。跨度>
    猜你喜欢
    • 2012-01-07
    • 2017-12-31
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2023-03-10
    • 2010-12-10
    相关资源
    最近更新 更多