【问题标题】:How to use Scrapy for URL crawling如何使用 Scrapy 进行 URL 抓取
【发布时间】:2018-05-30 10:56:13
【问题描述】:

我要爬取链接https://www.aparat.com/

我正确地抓取它并获取所有带有标题标签的视频链接;像这样:

import scrapy
class BlogSpider(scrapy.Spider):
    name = 'aparatspider'
    start_urls = ['https://www.aparat.com/']
    def parse(self, response):
        print '=' * 80 , 'latest-trend :'
        ul5 = response.css('.block-grid.xsmall-block-grid-2.small-block-grid-3.medium-block-grid-4.large-block-grid-5.is-not-center')
        ul5 = ul5.css('ul').css('li')
        latesttrend = []
        for li5 in ul5:
           latesttrend.append(li5.xpath('div/div[1]/a').xpath('@onmousedown').extract_first().encode('utf8'))
           print(latesttrend)

现在我的问题是:

如何获取داغ ترین ها标签中的所有链接,超过1000个?目前,我只得到 60 个,或多或少。

【问题讨论】:

  • 请分享一些您已经尝试过的示例以及您的代码当前所在的位置。
  • 我编辑我的问题

标签: python scrapy web-crawler


【解决方案1】:

我用以下代码做到了这一点:

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from scrapy.http import Request


class aparat_hotnewsItem(scrapy.Item):

      videourl = scrapy.Field()


class aparat_hotnewsSpider(CrawlSpider):
      name = 'aparat_hotnews'
      allowed_domains = ['www.aparat.com']
      start_urls = ['http://www.aparat.com/']

      # Xpath for selecting links to follow
      xp = 'your xpath'

      rules = (
    Rule(LinkExtractor(restrict_xpaths=xp), callback='parse_item', follow=True),
      )

      def parse_item(self, response):

      item = aparat_hotnewsItem()

      item['videourl'] = response.xpath('your xpath').extract()
      yield item

【讨论】:

    猜你喜欢
    • 2016-11-30
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 2023-04-03
    • 2016-07-16
    相关资源
    最近更新 更多