Python 刮痧蜘蛛不';不要刮东西

Python 刮痧蜘蛛不';不要刮东西,python,python-3.x,web-scraping,scrapy,Python,Python 3.x,Web Scraping,Scrapy,我正试图抓取这个网站,但蜘蛛没有返回任何加载的项目。我错过了什么 我用“刮壳”测试了XPath,它们似乎工作得很好 蜘蛛网: import scrapy from third_stage.items import ThirdStageItem from scrapy.loader import ItemLoader from scrapy.loader.processors import MapCompose from urllib.parse import urljoin class Bh

我正试图抓取这个网站,但蜘蛛没有返回任何加载的项目。我错过了什么

我用“刮壳”测试了XPath,它们似乎工作得很好

蜘蛛网:

import scrapy
from third_stage.items import ThirdStageItem
from scrapy.loader import ItemLoader
from scrapy.loader.processors import MapCompose
from urllib.parse import urljoin


class BhpSpider(scrapy.Spider):
    name = 'bhp'
    allowed_domains = ['web']
    start_urls = ['https://www.bhp.com/media-and-insights/reports-and- 
                  presentations?q0_r=category%3dAnnual%2bReports/',]

    def parse(self, response):
        i = ItemLoader(item=ThirdStageItem(), response=response)
        i.add_xpath('title', '//h2/a/text()')
        i.add_xpath('description', '//*[@class="col-9"]/p/text()')
        i.add_xpath('info_url', '//h2/a/@href', MapCompose(lambda i: 
                    urljoin(response.url, i)))
        return i.load_item()
项目:

import scrapy
from scrapy.item import Item
from scrapy.item import Field

class ThirdStageItem(Item):
    title = Field()
    description = Field()
    info_url = Field()
    pass
设置:

BOT_NAME = 'third_stage'
SPIDER_MODULES = ['third_stage.spiders']
NEWSPIDER_MODULE = 'third_stage.spiders'
ROBOTSTXT_OBEY = False
输出:

2019-07-14 16:53:34 [scrapy.core.engine] INFO: Spider opened
2019-07-14 16:53:34 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2019-07-14 16:53:34 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2019-07-14 16:53:34 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.bhp.com/media-and-insights/reports-and-presentations?q0_r=category%3dAnnual%2bReports/> (referer: None)
2019-07-14 16:53:34 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.bhp.com/media-and-insights/reports-and-presentations?q0_r=category%3dAnnual%2bReports/>
{}
2019-07-14 16:53:34 [scrapy.core.engine] INFO: Closing spider (finished)
2019-07-14 16:53:34 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 288,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 6601,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2019, 7, 14, 14, 53, 34, 791840),
 'item_scraped_count': 1,
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2019, 7, 14, 14, 53, 34, 528793)}
2019-07-14 16:53:34 [scrapy.core.engine] INFO: Spider closed (finished)
2019-07-1416:53:34[刮屑核心引擎]信息:蜘蛛打开
2019-07-14 16:53:34[scrapy.extensions.logstats]信息:抓取0页(以0页/分钟的速度),抓取0项(以0项/分钟的速度)
2019-07-14 16:53:34[scrapy.extensions.telnet]调试:telnet控制台监听127.0.0.1:6024
2019-07-14 16:53:34[scrapy.core.engine]调试:爬网(200)(参考:无)
2019-07-14 16:53:34[scrapy.core.scraper]调试:从
{}
2019-07-14 16:53:34[刮屑芯发动机]信息:关闭卡盘(已完成)
2019-07-14 16:53:34[垃圾统计]信息:倾销垃圾统计:
{'downloader/request_bytes':288,
“下载程序/请求计数”:1,
“downloader/request\u method\u count/GET”:1,
“downloader/response_字节”:6601,
“下载程序/响应计数”:1,
“下载程序/响应状态\计数/200”:1,
“完成原因”:“完成”,
“完成时间”:datetime.datetime(2019,7,14,14,53,34791840),
“物料刮取计数”:1,
“日志计数/调试”:3,
“日志计数/信息”:7,
“响应\u已接收\u计数”:1,
“调度程序/出列”:1,
“调度程序/出列/内存”:1,
“调度程序/排队”:1,
“调度程序/排队/内存”:1,
“开始时间”:datetime.datetime(2019,7,14,14,53,34528793)}
2019-07-14 16:53:34[刮屑芯发动机]信息:十字轴关闭(完成)

XPath在scrapy中似乎不起作用。我不确定您想要什么样的描述,但您可以在此xpath下找到标题和信息url:
response.xpath(//*[@class=“lvl1”]/li/a)
。 如果要获取多个项目(而不是包含所有数据的1个项目),可以如下更改解析方法:

def parse(self, response):
    xpath_urls = response.xpath('//*[@class="lvl1"]/li/a')
    for xpath_url in xpath_urls:
        i = ItemLoader(item=ThirdStageItem(), response=response)
        title = xpath_url.xpath('./text()').extract_first()
        info_url = xpath_url.xpath('./@href').extract_first()
        i.add_value('title', title)
        i.add_value('info_url', urljoin(response.url, info_url))
        yield i.load_item()
class ThirdStageItem(Item):
    title = Field()
    description = Field()
    info_url = Field()
    pass
您提供的物品也与您在蜘蛛中所做的不一致。应该看起来像这样:

def parse(self, response):
    xpath_urls = response.xpath('//*[@class="lvl1"]/li/a')
    for xpath_url in xpath_urls:
        i = ItemLoader(item=ThirdStageItem(), response=response)
        title = xpath_url.xpath('./text()').extract_first()
        info_url = xpath_url.xpath('./@href').extract_first()
        i.add_value('title', title)
        i.add_value('info_url', urljoin(response.url, info_url))
        yield i.load_item()
class ThirdStageItem(Item):
    title = Field()
    description = Field()
    info_url = Field()
    pass

非常感谢您抽出时间。我纠正了项目文件上的错误,我从错误的文件复制粘贴。你提供的解决方案是可行的,但是当我瞄准下面的报告列表时,它会从网页的标题/菜单中删除标题。!我想问题就出在你的起始url上:试试
https://www.bhp.com/media-and-insights/reports-and-presentations?q0_r=category%3dAnnual%2bReports
(因此,在没有附加/结尾的情况下)我修复了它与start\u URL的关系。非常感谢。