python - 如何收集具有不同链接的站点信息?
问题描述
我正在用 python 开发一个网络爬虫,我正在使用 Scrapy。对于这个网络爬虫,我需要一个站点,它包含广告的主页和一个我需要访问以获取信息的子页面。我怎么做 ?到目前为止,我已经开发了我将在下面发布的代码。我可以更多地实现什么,以便这个进入主页,获取信息,进入“子页面”获取信息并再次返回主页以获取其他公告的信息?谢谢。
代码:
import scrapy
class SapoSpider(scrapy.Spider):
name = "imo"
start_urls = [
'https://www.imovirtual.com/comprar/apartamento/lisboa/'
]
def parse(self, response):
for Property in response.css('div.offer-item-details'):
yield {
'preco': Property.css('span.offer-item
title::text').extract_first(),
'author': Property.css('li.offer-item-price::text').extract(),
'data': Property.css('li.offer-item-area::text').extract(),
'data': Property.css('li.offer-item-price-per-
m::text').extract(),
}
next_page = response.css('li.pager-next a::attr(href)').extract_first()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callback=self.parse)
解决方案
你可以做这样的事情(伪代码)
def parse(self, response):
for Property in response.css('div.offer-item-details'):
youritem = {
'preco': Property.css('span.offer-item title::text').extract_first(),
'author': Property.css('li.offer-item-price::text').extract(),
'data': Property.css('li.offer-item-area::text').extract(),
'data': Property.css('li.offer-item-price-perm::text').extract(),
}
# extract the sub page link
# load and parse it
yield scrapy.Request(subpage_link, callback=self.parse_subpage)
next_page = response.css('li.pager-next a::attr(href)').extract_first()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callback=self.parse)
def parse_subpage(self, youritem):
# here parse your subpage
# and complete your item information
yield youritem
推荐阅读
- sql - 如果 BigQuery 中包含文本,则计算值
- php - 如何使用 PHP 代码检测正在使用的浏览器作为 webview 应用内 MacOS?
- c# - 将 get, set for property 放在 Rider 的一条线上
- flutter - 如何从提供者 Flutter 中调用值
- java - 如何获取 ArrayList 中的特定列表或项目
- android - 如何从 tflite 文件中提取 labels.txt 文件?
- python - 从一个正在运行的脚本导入到另一个
- express - 是否可以使用唯一验证加密/解密 Sequelize 模型的字段?
- javascript - Type Annotations 只能在 TypeScript Files 中使用,在 REACT-NATIVE 中解析错误
- python - aplay:main:831:音频打开错误:没有这样的文件或目录