Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
undefined function error
#1
from scrapy import Spider
from scrapy.http import Request


class TesterSpider(Spider):
    name = 'tester'
    allowed_domains = ['books.toscrape.com']
    start_urls = ['http://books.toscrape.com/']


    def parse(self, response):
        books = response.xpath("//h3/a/@href").extract()
        for book in books:
            absolute_url = response.urljoin(book)
            yield Request(absolute_url, callback=self.parse_book)

        # process next page
        next_page_url = response.xpath("//a[text()='next']/@href").extract_first()
        absolute_next_page_url = response.urljoin(next_page_url)
        yield Request(absolute_next_page_url)


    def parse_book(self, response):
        title = response.xpath("//h1/text()").extract_first()
        price = response.xpath("//*[@class='price_color']/text()").extract_first()
        img_url = response.xpath("//img/@src").extract_first()
        img_url = img_url.replace('../..', 'https://books.toscrape.com')
        rating = response.xpath("//p[starts-with(@class,'star-rating')]/@class").extract_first()
        rating = rating.replace('star-rating ', '')
        desc = response.xpath("//div[(@id='product_description')]/following-sibling::p/text()").extract_first()
        
        # Product Description
        upc = product_desc(response, 'UPC')
        product_type = product_desc(response, 'Product Type')
        availability = product_desc(response, 'Availability')
        number_of_reviews = product_desc(response, 'Number of reviews')

        yield{
            'Title': title,
            'Price': price,
            'Location': img_url,
            'Rating': rating,
            'Description': desc,
            'UPC': upc,
            'Product Type': product_type,
            'Availability': availability,
            'Reviews': number_of_reviews
        }


    def product_desc(response, lookup):
        return response.xpath("//th[text()='" + lookup + "']/following-sibling::td/text()").extract_first()
As you can see, at the very bottom, the function 'product_desc' is defined, but just above that where I called it just above the yield block, my IDE, VS Code reports that it is undefined. Can anyone spot what I am missing.

Thank you
Reply
#2
Dedent product_desc so it becomes a function not a method of the class.
Reply
#3
(Sep-06-2022, 07:00 AM)Yoriz Wrote: Dedent product_desc so it becomes a function not a method of the class.


Right on the money. Thank you.
Reply
#4
(Sep-06-2022, 07:00 AM)Yoriz Wrote: Dedent product_desc so it becomes a function not a method of the class.

Is there a switch that I can pass to 'scrapy crawl SpiderName -o ExportFilePath' that will force an overwrite if the file previously existed?
Reply
#5
(Sep-06-2022, 12:25 PM)JonWayn Wrote: I there a switch that I can pass to 'scrapy crawl SpiderName -o ExportFilePath' that will force an overwrite if the file previously existed?
G:\div_code\scrapy_stuff
λ scrapy commands --help
Usage
=====
  scrapy commands

Options
=======
  -h, --help            show this help message and exit
  -a NAME=VALUE         set spider argument (may be repeated)
  -o FILE, --output FILE
                        append scraped items to the end of FILE (use - for stdout)
  -O FILE, --overwrite-output FILE
                        dump scraped items into FILE, overwriting any existing file
  -t FORMAT, --output-format FORMAT
                        format to use for dumping items

Global Options
--------------
  --logfile FILE        log file. if omitted stderr will be used
  -L LEVEL, --loglevel LEVEL
                        log level (default: DEBUG)
  --nolog               disable logging completely
  --profile FILE        write python cProfile stats to FILE
  --pidfile FILE        write process ID to FILE
  -s NAME=VALUE, --set NAME=VALUE
                        set/override setting (may be repeated)
  --pdb                 enable pdb on failure
So instead of -o pass inn -O.
scrapy crawl SpiderName -O ExportFilePath
Reply
#6
(Sep-06-2022, 01:59 PM)snippsat Wrote:
(Sep-06-2022, 12:25 PM)JonWayn Wrote: I there a switch that I can pass to 'scrapy crawl SpiderName -o ExportFilePath' that will force an overwrite if the file previously existed?
G:\div_code\scrapy_stuff
λ scrapy commands --help
Usage
=====
  scrapy commands

Options
=======
  -h, --help            show this help message and exit
  -a NAME=VALUE         set spider argument (may be repeated)
  -o FILE, --output FILE
                        append scraped items to the end of FILE (use - for stdout)
  -O FILE, --overwrite-output FILE
                        dump scraped items into FILE, overwriting any existing file
  -t FORMAT, --output-format FORMAT
                        format to use for dumping items

Global Options
--------------
  --logfile FILE        log file. if omitted stderr will be used
  -L LEVEL, --loglevel LEVEL
                        log level (default: DEBUG)
  --nolog               disable logging completely
  --profile FILE        write python cProfile stats to FILE
  --pidfile FILE        write process ID to FILE
  -s NAME=VALUE, --set NAME=VALUE
                        set/override setting (may be repeated)
  --pdb                 enable pdb on failure
So instead of -o pass inn -O.
scrapy crawl SpiderName -O ExportFilePath

Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Undefined Led_Zeppelin 4 1,429 Aug-02-2022, 11:57 AM
Last Post: buran
  ImportError: /home/pybind11_example.cpython-37m-x86_64-linux-gnu.so: undefined symbol chaitra 2 5,129 Feb-03-2021, 05:14 AM
Last Post: chaitra
  name "MyVariable" can be undefined Advisability 1 2,437 Aug-20-2020, 02:05 PM
Last Post: Gribouillis
  function error: undefined variable Atlantic_3000 3 6,965 Apr-12-2020, 02:53 AM
Last Post: bowlofred
  name undefined Coding Error Ewilliam51 2 2,148 Feb-06-2020, 12:19 AM
Last Post: Ewilliam51
  Error :unable to detect undefined names created in spyder ide error at line 2 milind_eac 2 8,294 Jul-30-2019, 10:29 PM
Last Post: milind_eac
  Timestamp is undefined ErnestTBass 7 7,970 Feb-16-2019, 08:27 PM
Last Post: snippsat
  python charmap codec can't decode byte X in position Y character maps to < undefined> owais 9 39,171 Apr-28-2018, 10:52 PM
Last Post: abadawi
  Error in using the output of one function in another function (beginner) MadsPJ 6 5,049 Mar-13-2017, 03:06 PM
Last Post: MadsPJ
  Python code with serial port and global undefined marciokoko 13 14,511 Jan-17-2017, 06:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020