Python Scrapy 没有遍历响应中的所有元素

问题描述 投票:0回答:0

本周刚开始使用scrapy。 今天选择BBC的网站作为练习。 即使我找到了我需要的所有信息,但解析函数中的 for-loop 只做了一次。 网上查了好几个sample,还是不明白

import scrapy
import logging
import openpyxl

class BbcchSpider(scrapy.Spider):
    name = "bbcch"
    allowed_domains = ["bbc.com"]
    start_urls = ["https://www.bbc.com/zhongwen/trad"]



    def parse(self, response):
        foundation = 'https://www.bbc.com/zhongwen/trad'

        for pageContent in response.xpath('//div[@class="bbc-1inew64 e4rwlwd0"]'):
            title = pageContent.css('a.focusIndicatorDisplayInlineBlock.bbc-1mirykb.ecljyjm0 span::text').get()
            url   = foundation + pageContent.css('h3.bbc-189hdql.ea6by782 a::attr(href)').get()
            time  = pageContent.css('time.bbc-1qkagz5.e1mklfmt0::text').get()
            articleBrief = pageContent.css('p.bbc-1f18rfa.ea6by781::text').get()

        yield {
            'title' : title,
            'articleBrief' : articleBrief,
            'url' : url,
            'time' : time
        }

#response.xpath('//div[@class="bbc-1inew64 e4rwlwd0"]') 这是所有新闻块所在的地方。 #selectors of title, url, time, articleBrief 是确定的。

运行爬虫后 它只从响应中获取第一个消息...

请指出我哪里遗漏或哪里错了。 谢谢。

期待获得列表中的所有新闻

python scrapy web-crawler
© www.soinside.com 2019 - 2024. All rights reserved.