Scrapy- 无法从 h3 中提取数据
Scrapy- can't extract data from h3
我开始使用 Scrapy,并设法提取了一些我需要的数据。然而,并不是所有的东西都是正确获得的。我正在应用 here, but it's not working. I've Googled around a bit, and also read 官方教程中的知识,但我很确定这不是这里的问题。
无论如何,我正在尝试解析来自 this webshop. I'm trying to obtain the product name, price, rrp, release date, category, universe, author and publisher. Here is the relevant CSS for one product: https://pastebin.com/9tqnjs7A 的产品信息。这是我的代码。末尾带有 #!
的所有内容均未按预期工作。
import scrapy
import pprint
class ForbiddenPlanetSpider(scrapy.Spider):
name = "fp"
start_urls = [
'https://forbiddenplanet.com/catalog/?q=mortal%20realms&sort=release-date&page=1',
]
def parse(self, response):
for item in response.css("section.zshd-00"):
print(response.css)
name = item.css("h3.h4::text").get() #!
price = item.css("span.clr-price::text").get() + item.css("span.t-small::text").get()
rrp = item.css("del.mqr::text").get()
release = item.css("dd.mzl").get() #!
category = item.css("li.inline-list__item::text").get() #!
universe = item.css("dt.txt").get() #!
authors = item.css("a.SubTitleItems").get() #!
publisher = item.css("dd.mzl").get() #!
pprint.pprint(dict(name=name,
price=price,
rrp=rrp,
release=release,
category=category,
universe=universe,
authors=authors,
publisher = publisher
)
)
我想我需要添加一些子搜索(例如,目前发布和发布者具有相同的标准),但我不知道如何用词来搜索它(我试过,但最终得到了不涵盖它的通用教程)。任何指向我正确方向的东西都将不胜感激!
哦,我没有包含 ' ' 空格,因为每当我使用一个 Scrapy 时,立即找不到。
Scrapy不渲染JS,试试disable javascript in your browser and refresh the page, the HTML structure is different for site version without JS.
您应该使用新的 HTML 结构重写您的选择器。尝试使用 XPATH 而不是 CSS 它更加灵活。
UPD
抓取此网站的最简单方法是向 https://forbiddenplanet.com/api/products/listing/?q=mortal%20realms&sort=release-date
发出请求
响应是一个包含所有必要数据的 JSON 对象。您可以将“结果”字段(或整个 JSON 对象)转换为 python 字典并使用字典方法获取所有字段。
一份有效并展示想法的代码草稿。
import scrapy
import json
def get_tags(tags: list):
parsed_tags = []
if tags:
for tag in tags:
parsed_tags.append(tag.get('name'))
return parsed_tags
return None
class ForbiddenplanetSpider(scrapy.Spider):
name = 'forbiddenplanet'
allowed_domains = ['forbiddenplanet.com']
start_urls = ['https://forbiddenplanet.com/api/products/listing/?q=mortal%20realms&sort=release-date']
def parse(self, response):
response_dict = json.loads(response.body)
items = response_dict.get('results')
for item in items:
yield {
'name': item.get('title'),
'price': item.get('site_price'),
'rrp': item.get('rrp'),
'release': item.get('release_date'),
'category': get_tags(item.get('derived_tags').get('type')),
'universe': get_tags(item.get('derived_tags').get('universe')),
'authors': get_tags(item.get('derived_tags').get('author')),
'publisher': get_tags(item.get('derived_tags').get('publisher')),
}
next_page = response_dict.get('next')
if next_page:
yield scrapy.Request(
url=next_page,
callback=self.parse
)
我开始使用 Scrapy,并设法提取了一些我需要的数据。然而,并不是所有的东西都是正确获得的。我正在应用 here, but it's not working. I've Googled around a bit, and also read
无论如何,我正在尝试解析来自 this webshop. I'm trying to obtain the product name, price, rrp, release date, category, universe, author and publisher. Here is the relevant CSS for one product: https://pastebin.com/9tqnjs7A 的产品信息。这是我的代码。末尾带有 #!
的所有内容均未按预期工作。
import scrapy
import pprint
class ForbiddenPlanetSpider(scrapy.Spider):
name = "fp"
start_urls = [
'https://forbiddenplanet.com/catalog/?q=mortal%20realms&sort=release-date&page=1',
]
def parse(self, response):
for item in response.css("section.zshd-00"):
print(response.css)
name = item.css("h3.h4::text").get() #!
price = item.css("span.clr-price::text").get() + item.css("span.t-small::text").get()
rrp = item.css("del.mqr::text").get()
release = item.css("dd.mzl").get() #!
category = item.css("li.inline-list__item::text").get() #!
universe = item.css("dt.txt").get() #!
authors = item.css("a.SubTitleItems").get() #!
publisher = item.css("dd.mzl").get() #!
pprint.pprint(dict(name=name,
price=price,
rrp=rrp,
release=release,
category=category,
universe=universe,
authors=authors,
publisher = publisher
)
)
我想我需要添加一些子搜索(例如,目前发布和发布者具有相同的标准),但我不知道如何用词来搜索它(我试过,但最终得到了不涵盖它的通用教程)。任何指向我正确方向的东西都将不胜感激!
哦,我没有包含 ' ' 空格,因为每当我使用一个 Scrapy 时,立即找不到。
Scrapy不渲染JS,试试disable javascript in your browser and refresh the page, the HTML structure is different for site version without JS.
您应该使用新的 HTML 结构重写您的选择器。尝试使用 XPATH 而不是 CSS 它更加灵活。
UPD
抓取此网站的最简单方法是向 https://forbiddenplanet.com/api/products/listing/?q=mortal%20realms&sort=release-date
响应是一个包含所有必要数据的 JSON 对象。您可以将“结果”字段(或整个 JSON 对象)转换为 python 字典并使用字典方法获取所有字段。
一份有效并展示想法的代码草稿。
import scrapy
import json
def get_tags(tags: list):
parsed_tags = []
if tags:
for tag in tags:
parsed_tags.append(tag.get('name'))
return parsed_tags
return None
class ForbiddenplanetSpider(scrapy.Spider):
name = 'forbiddenplanet'
allowed_domains = ['forbiddenplanet.com']
start_urls = ['https://forbiddenplanet.com/api/products/listing/?q=mortal%20realms&sort=release-date']
def parse(self, response):
response_dict = json.loads(response.body)
items = response_dict.get('results')
for item in items:
yield {
'name': item.get('title'),
'price': item.get('site_price'),
'rrp': item.get('rrp'),
'release': item.get('release_date'),
'category': get_tags(item.get('derived_tags').get('type')),
'universe': get_tags(item.get('derived_tags').get('universe')),
'authors': get_tags(item.get('derived_tags').get('author')),
'publisher': get_tags(item.get('derived_tags').get('publisher')),
}
next_page = response_dict.get('next')
if next_page:
yield scrapy.Request(
url=next_page,
callback=self.parse
)