📅  最后修改于: 2023-12-03 15:41:26.617000             🧑  作者: Mango
在使用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}
//a/@href
: 选取所有<a>
标签下的href属性。
运行爬虫后,可以获取到所有页面上的链接地址。