如何使用 Scrapy 从网站上抓取 JavaScript 渲染数据?
How to scrape JavaScript rendered data from a website using Scrapy?
使用 Scrapy,我正在尝试抓取标签 <script type="application/ld+json">....
的数据
import json
class TestSpider(scrapy.Spider):
name = 'content'
start_urls = ['https://www.maserati.com/us/en/models/ghibli']
def parse(self, response):
for content in response.xpath('(//script[@type="application/ld+json"])/text()'):
data = json.loads(content)
yield {
'name': data['name'],
}
next_page = response.css('li.next a::attr("href")').get()
if next_page is not None:
yield response.follow(next_page, self.parse)
但是,我在终端 scrapy runspider test_spider.py - O test1.jl
中写入后没有得到我期望的 test1.jl 文件
我只是想知道它是如何工作的。
图片和网址link如下供查阅:
显示 javascript 标签和我想要生成的名称 属性 的图像
我的代码和终端中的代码的图像
你是如此亲密...只是想念 getall()
import scrapy
import json
class TestSpider(scrapy.Spider):
name = 'content'
start_urls = ['https://www.maserati.com/us/en/models/ghibli']
def parse(self, response):
for content in response.xpath('(//script[@type="application/ld+json"])/text()').getall():
data = json.loads(content)
yield {
'name': data['name'],
}
next_page = response.css('li.next a::attr("href")').get()
if next_page is not None:
yield response.follow(next_page, self.parse)
(虽然我没有看到任何“下一步”按钮)
使用 Scrapy,我正在尝试抓取标签 <script type="application/ld+json">....
import json
class TestSpider(scrapy.Spider):
name = 'content'
start_urls = ['https://www.maserati.com/us/en/models/ghibli']
def parse(self, response):
for content in response.xpath('(//script[@type="application/ld+json"])/text()'):
data = json.loads(content)
yield {
'name': data['name'],
}
next_page = response.css('li.next a::attr("href")').get()
if next_page is not None:
yield response.follow(next_page, self.parse)
但是,我在终端 scrapy runspider test_spider.py - O test1.jl
中写入后没有得到我期望的 test1.jl 文件
我只是想知道它是如何工作的。
图片和网址link如下供查阅:
显示 javascript 标签和我想要生成的名称 属性 的图像
我的代码和终端中的代码的图像
你是如此亲密...只是想念 getall()
import scrapy
import json
class TestSpider(scrapy.Spider):
name = 'content'
start_urls = ['https://www.maserati.com/us/en/models/ghibli']
def parse(self, response):
for content in response.xpath('(//script[@type="application/ld+json"])/text()').getall():
data = json.loads(content)
yield {
'name': data['name'],
}
next_page = response.css('li.next a::attr("href")').get()
if next_page is not None:
yield response.follow(next_page, self.parse)
(虽然我没有看到任何“下一步”按钮)