只有两个简单的疑问:
1-我希望我的最终JSON文件替换文本提取(例如,提取的文本是添加到购物车,但我想在我的最终JSON中更改为IN STOCK。有可能吗?
2-我还想添加一些自定义数据到我的最终JSON文件中,这些数据不在网站中,例如“商店名称”……所以我刮的每个产品后面都会有店名。有可能吗?
我同时使用Portia和Scrapy,所以在这两个平台上都欢迎您的建议。
我的Scrapy爬虫代码如下:
import scrapy
from __future__ import absolute_import
from scrapy import Request
from scrapy.linkextractors import LinkExtractor
from scrapy.loader import ItemLoader
from scrapy.loader.processors import Identity
from scrapy.spiders import Rule
from ..utils.spiders import BasePortiaSpider
from ..utils.starturls import FeedGenerator, FragmentGenerator
from ..utils.processors import Item, Field, Text, Number, Price, Date, Url,
Image, Regex
from ..items import PortiaItem
class Advent(BasePortiaSpider):
name = "advent"
allowed_domains = [u'www.adventgames.com.au']
start_urls = [u'http://www.adventgames.com.au/c/4504822/1/all-games-a---k.html',
{u'url': u'http://www.adventgames.com.au/Listing/Category/?categoryId=4504822&page=[1-5]',
u'fragments': [{u'valid': True,
u'type': u'fixed',
u'value': u'http://www.adventgames.com.au/Listing/Category/?categoryId=4504822&page='},
{u'valid': True,
u'type': u'range',
u'value': u'1-5'}],
u'type': u'generated'}]
rules = [
Rule(
LinkExtractor(
allow=('.*'),
deny=()
),
callback='parse_item',
follow=True
)
]
items = [
[
Item(
PortiaItem,
None,
u'.DataViewCell > form > table',
[
Field(
u'Title',
'tr:nth-child(1) > td > .DataViewItemProductTitle > a *::text',
[]),
Field(
u'Price',
'tr:nth-child(1) > td > .DataViewItemOurPrice *::text',
[]),
Field(
u'Img_src',
'tr:nth-child(1) > td > .DataViewItemThumbnailImage > div > a > img::attr(src)',
[]),
Field(
u'URL',
'tr:nth-child(1) > td > .DataViewItemProductTitle > a::attr(href)',
[]),
Field(
u'Stock',
'tr:nth-child(2) > td > .DataViewItemAddToCart > .wButton::attr(value)',
[])])]]发布于 2018-04-09 22:59:12
我从未使用过items类变量,它看起来非常难以阅读和理解。
我建议您有一个回调方法,并像这样解析它
def my_callback_func(self, response):
myitem = PortiaItem()
for item in response.css(".DataViewCell > form > table"):
item['Title'] = item.css('tr:nth-child(1) > td > .DataViewItemProductTitle > a *::text').extract_first()
item['Stock'] = item.css('tr:nth-child(2) > td > .DataViewItemAddToCart > .wButton::attr(value)').extract_first()
if item['Stock'] == "ADD TO CART":
item['is_available'] = "YES"
...... and so on
yield itemhttps://stackoverflow.com/questions/49731142
复制相似问题