📜  获取 href scrapy xpath - Python (1)

📅  最后修改于: 2023-12-03 15:41:26.617000             🧑  作者: Mango

获取 href scrapy xpath - Python

在使用scrapy爬虫时,我们需要经常获取HTML页面上的链接地址。这里我们介绍如何使用xpath语法获取页面上的href链接。

导入模块
import scrapy
爬虫类
class MySpider(scrapy.Spider):
    name = 'my_spider'
    start_urls = ['http://example.com']

    def parse(self, response):
        for link in response.xpath('//a/@href'):
            href = link.extract()
            yield {'href': href}
xpath语法解释

//a/@href : 选取所有<a>标签下的href属性。

效果

运行爬虫后,可以获取到所有页面上的链接地址。