how to change scrapy user agent without settings file

Viewed 2668

I have implemented my spider via a script just like the main example:

import scrapy

class BlogSpider(scrapy.Spider):
    name = 'blogspider'
    start_urls = ['https://blog.scrapinghub.com']

    def parse(self, response):
        for title in response.css('h2.entry-title'):
            yield {'title': title.css('a ::text').extract_first()}

        next_page = response.css('div.prev-post > a ::attr(href)').extract_first()
        if next_page:
            yield scrapy.Request(response.urljoin(next_page), callback=self.parse)

I run with:

scrapy runspider myspider.py

How do I change the user agent if I don't have a setting or have create from startproject? As specified here:

https://doc.scrapy.org/en/latest/topics/settings.html

2 Answers
Related