search

Home  >  Q&A  >  body text

python - scrapy obtains the specified content of the web page, then flips to the next page to continue, with a fixed number of cycles. . question

import scrapy
from movie.items import MovieItem
 
class MeijuSpider(scrapy.Spider):
    name = "meiju"
    allowed_domains = ["alexa.cn"]
    start_urls = ['www.alexa.cn/siterank']
 
def parse(self, response):
    movies = response.xpath('//ul[@class="siterank-sitelist"]/li')
    for each_movie in movies:
        item = MovieItem()
        item['name'] =each_movie.xpath('.//p[@class="infos"]').extract()[0]
        yield item

The code is like this. What I want to capture in a loop is:

www.alexa.cn/siterank/2
www.alexa.cn/siterank/3
www.alexa.cn/siterank/4
...

I think the loop should be like this for i in range(2,10):
yield scrapy.Request('www.alexa.cn/siterank/%d'%i), but I don’t know how to fill it in Go in. Help

天蓬老师天蓬老师2731 days ago856

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-06-12 09:21:33

    If you are sure about the scope, it is better to start with start_urls

    start_urls = ['http://www.alexa.cn/siterank/{n}'.format(n=x) for x in range(2,10)] 

    reply
    0
  • 学习ing

    学习ing2017-06-12 09:21:33

    There are examples on the official website. Regarding tracking the next page, the examples on the official website use recursion. The code on the official website is as follows:

    import scrapy
    
    
    class QuotesSpider(scrapy.Spider):
        name = "quotes"
        start_urls = [
            'http://quotes.toscrape.com/page/1/',
        ]
    
        def parse(self, response):
            for quote in response.css('p.quote'):
                yield {
                    'text': quote.css('span.text::text').extract_first(),
                    'author': quote.css('small.author::text').extract_first(),
                    'tags': quote.css('p.tags a.tag::text').extract(),
                }
            
            # next_page是用css选择器获取到的下一页, 在下面它递归地调用了parse方法来不断地追踪下一页
            next_page = response.css('li.next a::attr(href)').extract_first()
            if next_page is not None:
                next_page = response.urljoin(next_page)
                yield scrapy.Request(next_page, callback=self.parse)

    I used Scrapy to write a Tieba crawler. I also used this recursive method to get the next page. The code is as follows:

    import scrapy
    from tieba_crawler.items import ImageItem
    
    
    class TiebaSpider(scrapy.Spider):
        name = 'tbimg'
    
        def start_requests(self):
            url = 'http://tieba.baidu.com/f?kw=%E6%B8%A1%E8%BE%B9%E9%BA%BB%E5%8F%8B'
            yield scrapy.Request(url=url, callback=self.parse_post)
    
        def parse_post(self, response):
            post_list = response.css('ul#thread_list li.j_thread_list')
            for item in post_list:
                title = item.css('a.j_th_tit::text').extract_first()
                url = 'http://tieba.baidu.com' \
                  + item.css('a.j_th_tit::attr(href)').extract_first()
                yield scrapy.Request(url=url, callback=self.parse_image)
            page_list = response.css('p#frs_list_pager a::attr(href)').extract()
            if not page_list:
                return
            else:
                next_page = page_list[-2]
                if next_page:
                    yield response.follow(next_page, callback=self.parse_post)
    
        def parse_image(self, response):
            img_urls = response.css('p#j_p_postlist img.BDE_Image::attr(src)').extract()
            yield ImageItem(image_urls=img_urls)
            page_list = response.css('ul.l_posts_num li.pb_list_pager a::attr(href)').extract()
            if not page_list:
                return
            else:
                next_page = page_list[-2]
                if next_page:
                    yield response.follow(next_page, callback=self.parse_image)

    reply
    0
  • Cancelreply