Scrape All External Links from Multiple URLs in a Text File with Scrapy
<p>I am new to Scrapy and Python and as such I am a beginner. I want to be able to have Scrapy read a text file with a seed list of around 100k urls, have Scrapy visit each URL, and extract all external URLs (URLs of Other Sites) found on each of those Seed URLs and export the results to a separate text file.</p>

<p>Scrapy should only visit the URLs in the text file, not spider out and follow any other URL.</p>

<p>I want to be able to have Scrapy work as fast as possible, I have a very powerful server with a 1GBS line. Each URL in my list is from a unique domain, so I won't be hitting any 1 site hard at all and thus won't be encountering IP blocks.</p>

<p>How would I go about creating a project in Scrapy to be able to extract all external links from a list of urls stored in a textfile?</p>

<p>Thanks.</p>

<p>You should use: <br>
1. start_requests function for reading list of urls. <br>
2. css or xpath selector for all "a" html elements.</p>

<pre><code>from scrapy import Spider

class YourSpider(Spider):
    name = "your_spider"

    def start_requests(self):
        with open('your_input.txt', 'r') as f:  # read the list of urls
           for url in f.readlines()             # process each of them
               yield Request(url, callback=self.parse)

    def parse(self, response):
        item = YourItem(parent_url=response.url)
        item['child_urls'] = response.css('a::attr(href)').extract()
        return item
</code></pre>

<p>More info about start_requests here: <br>
<a href="http://doc.scrapy.org/en/latest/topics/spiders.html#scrapy.spiders.Spider.start_requests" rel="nofollow">http://doc.scrapy.org/en/latest/topics/spiders.html#scrapy.spiders.Spider.start_requests</a></p>

<p>For extracting scraped items to another file use Item Pipeline or Feed Export. Basic pipeline example here: <br>
<a href="http://doc.scrapy.org/en/latest/topics/item-pipeline.html#write-items-to-a-json-file" rel="nofollow">http://doc.scrapy.org/en/latest/topics/item-pipeline.html#write-items-to-a-json-file</a></p>

python url web-scraping scrapy web-crawler