You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3问题:类外部调用类方法失败,寻求技术帮助

Troubleshooting OOP Refactor for Your Indeed Scraper

Hey there! I totally get how frustrating it is when you’re trying to refactor a perfectly working scraper into OOP and hit a wall—especially after hours of digging for solutions that don’t fit your exact use case. Let’s work through this together.

First off, the code snippet you shared is incomplete (your __init__ cuts off at self.max_pages =...), and you haven’t shown the problematic for loop where you’re calling the class method from outside the class. That said, here are the most common pitfalls people run into with this exact scenario, plus a working example to reference:

Common Issues & Fixes

  • Calling instance methods without an instance:
    If your method is an instance method (the default, no decorator), you can’t call it directly via IndeedScraper.your_method() from outside the class. You need to first create an instance of the scraper, then call the method on that instance.

  • Forgetting self in instance method definitions:
    All instance methods need self as their first parameter to access the class’s attributes (like self.role or self.max_pages). Missing this will throw a positional argument error.

  • Misusing class methods:
    If you intended to use a class method (shared across all instances), you need to mark it with the @classmethod decorator and use cls instead of self as the first parameter.

Working Example Reference

Here’s a stripped-down version of how your OOP scraper should look, including a for loop both inside and outside the class:

class IndeedScraper(object):
    def __init__(self, role, max_pages):
        self.role = role
        self.max_pages = max_pages
        # Add other init setup here (e.g., requests session, headers)

    def fetch_single_page(self, page_number):
        # Example logic to fetch a page
        print(f"Scraping page {page_number} for {self.role}")
        # Return scraped data here (e.g., job listings)

    def scrape_all_pages(self):
        # Internal loop calling the instance method
        for page in range(1, self.max_pages + 1):
            self.fetch_single_page(page)

# How to call from outside the class
if __name__ == "__main__":
    # Create an instance of the scraper
    my_scraper = IndeedScraper("software engineer", 5)
    
    # Option 1: Call the wrapper method that handles the loop internally
    my_scraper.scrape_all_pages()

    # Option 2: Run the loop outside the class and call the method each time
    for page_num in range(1, my_scraper.max_pages + 1):
        my_scraper.fetch_single_page(page_num)

To Help You Further

Could you share:

  • The full code for your IndeedScraper class (including the method you’re trying to call)
  • The exact for loop code where you’re hitting the issue
  • Any error messages you’re getting (tracebacks are super helpful!)

With that info, we can pinpoint exactly what’s going wrong.

内容的提问来源于stack exchange,提问作者pyrish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:41:09