python - Scrapy - 选择和抓取特定类型的站点地图节点
问题描述
这是我正在抓取的网站的站点地图。第 3 和第 4<sitemap>
个节点具有指向项目详细信息的 url。有没有办法仅将爬取逻辑应用于这些节点?(比如通过索引选择它们)
class MySpider(SitemapSpider):
name = 'myspider'
sitemap_urls = [
'https://www.dfimoveis.com.br/sitemap_index.xml',
]
sitemap_rules = [
('/somehow targeting the 3rd and 4th node', 'parse_item')
]
def parse_item(self, response):
# scraping the item
解决方案
您不需要使用SitemapSpider,只需使用正则表达式和标准蜘蛛。
def start_requests(self):
sitemap = 'https://www.dfimoveis.com.br/sitemap_index.xml'
yield scrapy.Request(url=sitemap, callback=self.parse_sitemap)
def parse_sitemap(self, response):
sitemap_links = re.findall(r"<loc>(.*?)</loc>", response.text, re.DOTALL)
sitemap_links = sitemap_links[2:4] # Only 3rd and 4th nodes.
for sitemap_link in sitemap_links:
yield scrapy.Request(url=sitemap_link, callback=self.parse)
推荐阅读
- java - 如何在 bitcoinj java 或 kotlin 中从比特币的负私钥生成正私钥
- python - pylatex - 从文件夹中的 tex、bib 和图像构建 .pdf
- replication - 合并复制订阅者尝试从旧快照文件夹加载快照文件
- amazon-web-services - S3EventNotification.S3EventNotificationRecord 中的 getEventTime() 与 S3 对象的 LastModified
- go - 如何防止 gofmt 添加`//go:build test`
- java - 配置 Selenium 驱动程序以使用 Socks5 代理
- ssh - 使用vscode远程ssh失败,但是直接使用ssh可以工作
- time-series - 通过不稳定的网络将数据写入时间序列数据库
- python - 仅当我的代码中的变量等于另一个变量时,如何让我的不和谐机器人发送消息?
- python - 如何使用函数从宽数据框中创建多个子图