Scrapy read list of URLs from file to scrape?

后端 未结 3 1737
执念已碎
执念已碎 2020-12-23 18:16

I\'ve just installed scrapy and followed their simple dmoz tutorial which works. I just looked up basic file handling for python and tried to get the crawler to read a list

3条回答
  •  盖世英雄少女心
    2020-12-23 18:54

    You were pretty close.

    f = open("urls.txt")
    start_urls = [url.strip() for url in f.readlines()]
    f.close()
    

    ...better still would be to use the context manager to ensure the file's closed as expected:

    with open("urls.txt", "rt") as f:
        start_urls = [url.strip() for url in f.readlines()]
    

提交回复
热议问题