使用砂纸从无限滚动页面上刮擦数据
我是网络刮擦的新手,我想从网站上刮擦所有产品的信息。
我已经写了一个示例代码以scrape数据为:
def start_requests(self):
urls = [
'https://www.trendyol.com/camasir-deterjani-x-c108713',
'https://www.trendyol.com/yumusaticilar-x-c103814',
'https://www.trendyol.com/camasir-suyu-x-c103812',
'https://www.trendyol.com/camasir-leke-cikaricilar-x-c103810',
'https://www.trendyol.com/camasir-yan-urun-x-c105534',
'https://www.trendyol.com/kirec-onleyici-x-c103806',
'https://www.trendyol.com/makine-kirec-onleyici-ve-temizleyici-x-c144512'
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse, meta=meta, dont_filter=True)
def parse(self, response):
soup = BeautifulSoup(response.text, 'lxml')
data = re.search(r"__SEARCH_APP_INITIAL_STATE__=(.*?});", response.text)
data = json.loads(data.group(1))
for p in data["products"]:
item=TeknosaItem()
item['rowid'] = hash(str(datetime.datetime.now()) + str(p["id"]))
item['date'] = str(datetime.datetime.now())
item['listing_id'] = p["id"]
item['product_id'] = p["id"]
item['product_name'] = p["name"]
item['price'] = p["price"]["sellingPrice"]
item['url'] = p["url"]
yield item
我编写的代码能够在第一页上列出的所有产品中刮擦数据,但是当我们向下滚动页面时,页面通过Ajax Get get请求动态加载更多数据,并且无法刮擦该数据。我已经观看了一些视频并阅读了一些文章,但是我无法弄清楚如何滚动数据滚动时生成的数据。对此的任何帮助将不胜感激。
我在目标网站上找到了无限页面示例:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不使用零食,购买您可以调整下一个示例如何从类别中获取所有产品(使用其Ajax API):
这将从类别中获取所有产品:
I don't use Scrapy, buy you can adjust next example how to get all products from the category (using their Ajax API):
This will get all products from the category:
老实说,我认为最好的方法是从API获取信息,但我想回答有关分页的问题。
因此,您可以看到滚动滚动时会更改URL(
?pi = pagenumber
),因此我们可以循环浏览页面,当我们到达不存在的页面(404状态)时,我们将处理状态代码,并从循环中断。So honestly I think the best way is to get the info from the API, but I wanted to answer you question about pagination.
So you can see when you scroll that the url changes (
?pi=pagenumber
), so we can loop through the pages, and when we get to a page that doesn't exist (404 status), we'll handle the status code and break from the loop.