使用Selenium和Scrapy调用Python中的方法

2024-07-01 06:52:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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

Tags: 方法selfcomurlforuiddateby
1条回答
网友
1楼 · 发布于 2024-07-01 06:52:07

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

parsequestion()返回生成器。如果您希望parse()最终返回{}返回的项,则需要

yield self.parsequestion(uid,url,date)

相反。在

另外,itemquest和{}在任何地方都没有定义,因此如果这是整个代码,那么将生成错误。在

相关问题 更多 >

    热门问题