2017-12-25 33 views
0

사이트가 있으며 로고를 긁어 내고 싶습니다.사이트 로고 긁기

문제 : 나는 로고에 대한 모든 데이터를 저장하는 외부 클래스가

- URL에 링크를, 모든 것이 정확하고있다 : 문제가있는

class PatternUrl: 

    def __init__(self, path_to_img="", list_of_conditionals=[]): 
     self.url_pattern = "" 
     self.file_url = "" 
     self.path_to_img = path_to_img 
     self.list_of_conditionals = list_of_conditionals 

    def find_obj(self, response): 
     for el in self.list_of_conditionals: 
      if el: 
       if self.path_to_img: 
        url = response 
        file_url = str(self.path_to_img) 
        print(file_url) 
        yield LogoScrapeItem(url=url, file_url=file_url) 

class LogoSpider(scrapy.Spider): 
.... 
def parse(self, response): 
     a = PatternUrl(response.css("header").xpath("//a[@href='"+response.url+'/'+"']/img/@src").extract_first(), [response.css("header").xpath("//a[@href='"+response.url+'/'+"']")]) 
     a.find_obj(response) 

yield lines

어떤 이유로이 줄을 주석 처리하면이 방법의 모든 줄이 실행되고 있습니다. 수율이 논평된다

출력 :

#yield LogoScrapeItem(url=url, file_url=file_url) 

2017-12-25 11:09:32 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://time.com> (referer: None) 
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAAAyCAYAAAD........ 
2017-12-25 11:09:32 [scrapy.core.engine] INFO: Closing spider (finished) 
2017-12-25 11:09:32 [scrapy.statscollectors] INFO: Dumping Scrapy stats: 

출력 수율 을 논평되지 :

yield LogoScrapeItem(url=url, file_url=file_url) 

2017-12-25 11:19:28 [scrapy.core.engine] INFO: Spider opened 
2017-12-25 11:19:28 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 
2017-12-25 11:19:28 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024 
2017-12-25 11:19:28 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://git-scm.com/robots.txt> (referer: None) 
2017-12-25 11:19:28 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://git-scm.com/docs/git-merge> (referer: None) 
2017-12-25 11:19:28 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://time.com/robots.txt> (referer: None) 
2017-12-25 11:19:29 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://time.com> (referer: None) 
2017-12-25 11:19:29 [scrapy.core.engine] INFO: Closing spider (finished) 
2017-12-25 11:19:29 [scrapy.statscollectors] INFO: Dumping Scrapy stats: 
{'downloader/request_bytes': 926, 

질문 :

이 함수가 실행되지 않는 경우가 항복 계산서, 왜 ?

답변

1

수율 발전기를 생산하도록 설계되었습니다. 대신

for x in a.find_obj(response): 

: 당신이로 find_obj를 실행해야합니다 같은

는 것 같습니다.

자세한 내용은 What does the "yield" keyword do?

을 참조하십시오.
1

find_obj 메서드는 실제로 yield 키워드 때문에 생성자입니다. 발전기에 대한 자세한 설명은 yieldthis StackOverflow question을 권장합니다.

은 당신의 방법의 결과를 얻기 위하여 당신이 유사한 방식으로 호출해야 년 :

for logo_scrape_item in a.find_obj(response): 
    # perform an action on your logo_scrape_item