简体   繁体   中英

Scrapy not able to go to next page

I am learning how to use Scrappy and am trying to make a crawler that scrapes website link and text from it. My crawler works for http://quotes.toscrape.com/ and http://books.toscrape.com/ but not for real life examples like https://pypi.org/project/wikipedia/ or Wikipedia. I am not able to understand what is causing this. Please help me

MyCode

import scrapy
from scrapy.crawler import CrawlerRunner
from scrapy.utils.project import get_project_settings
from twisted.internet import reactor
from scrapy.utils.log import configure_logging

class firstSpider(scrapy.Spider):
    name = "htmlcrawler"
    start_urls = [
        'https://pypi.org/project/wikipedia/',
    ]

    def parse(self, response):
        val1=response.css("p.text::text").extract_first()
        val2=response.css("span.text::text").extract_first()
        val3=response.css("pre.text::text").extract_first()
        text = str("" if val3 is None else val3) + str("" if val2 is None else val2)+str("" if val1 is None else val1)
        NEXT_PAGE_SELECTOR = '.next a ::attr(href)'
        next_page = response.css(NEXT_PAGE_SELECTOR).extract_first()
        print(next_page)
        if next_page:
           next_page = response.urljoin(next_page)
           yield{'html':next_page,'text':text}
           yield scrapy.Request(next_page, callback=self.parse)

def run():
    settings = get_project_settings()
    settings.set('FEED_FORMAT', 'json')
    settings.set('FEED_URI', 'result.json')
    settings.set('Depth_Limit',60)
    settings.set('DOWNLOAD_DELAY',2)
    settings.set('DUPEFILTER_CLASS','scrapy.dupefilters.BaseDupeFilter')

    configure_logging()
    runner = CrawlerRunner(settings)
    runner.crawl(firstSpider)

    d = runner.join()
    d.addBoth(lambda _: reactor.stop())

    reactor.run()
if __name__=="__main__":
    run()

I am running scrappy from Atom Hydrogen.

Edit

I changed the dupe filter class and tried to make some changes to my link gatherer from https://blog.siliconstraits.vn/building-web-crawler-scrapy/ but it still isnt working .

It's crawling, but stops because you are sending requests for the same page ( #content ).

Scrapy has DupeFilter enabled by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM