Skip to main content
 首页 » 编程设计

python之使用 Selenium 和 Scrapy 在 Python 中调用方法

2025年05月04日11thcjp

我正在尝试使用 Scrapy 抓取一个网站,我想调用一个类中的方法。但是方法没有被调用,奇怪的是没有报错。 感兴趣的方法是 parsequestion,由 for 循环内的 parse 函数调用。 谢谢

class QuoraSpider(scrapy.Spider): 
    name = "quora" 
    allowed_domains = ["quora.com"] 
    BASE_URL = 'https://quora.com/' 
 
def __init__(self): 
 
 
    self.driver = webdriver.Chrome('C:\Users\iaffa\Desktop\chromedriver.exe') 
 
    self.start_urls = ["https://www.quora.com/Computer-Programming/all_questions"] 
 
    print "Getting Answer" 
 
    self.driver.get("http://www.quora.com/") 
    #LOG IN 
 
def parse(self, response) 
    wait = WebDriverWait(self.driver, 30) 
    post_elems = self.driver.find_elements_by_class_name("pagedlist_item") 
 
    i=1 
    url_list = [] 
    date_time_list = [] 
 
    for post in post_elems: 
 
        Text = post.find_element_by_xpath('.//span[contains(@class,"question_text")]').text 
        url_list.append(post.find_element_by_xpath('.//a[contains(@class,"question_link")]').get_attribute('href')) 
        date_time_list.append(post.find_element_by_xpath('.//span[contains(@class,"timestamp")]/a').text) 
 
 
    uid =1 
    for (url,date) in zip(url_list,date_time_list): 
        self.parsequestion(uid,url,date) ##I can't call this function 
        uid += 1 
 
 
def parsequestion(self,uid,url,date): #the function that i want 
 
    self.driver.get(url) 
 
 
    #create item quest 
    yield itemquest 
 
    #create item ans 
    yield itemans 

请您参考如下方法:

你确定这个函数没有被调用吗?该函数可能已被调用,但您没有对返回值执行任何操作。

parsequestion() 返回一个生成器。如果你想让parse()最后返回parsequestion()返回的项,你需要

yield self.parsequestion(uid,url,date) 

相反。

此外,itemquest 和 itemans 未在任何地方定义,因此如果这是整个代码,则会产生错误。