在scrapy的项目管道中访问蜘蛛的响应

时间:2015-06-25 11:11:16

标签: python scrapy

我有像蜘蛛一样的

public static class HomeFragment extends Fragment implements OnRefreshListener{
public static final String ARG_CATEGORY_NUMBER = "category_number";
private UiLifecycleHelper uiHelper;
public int currentimageindex = 0;
private SwipeRefreshLayout swipeLayout;

public HomeFragment() {
    // Empty constructor required for fragment subclasses
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container,
            false);

    //SWIPE TO REFRESH
    swipeLayout = (SwipeRefreshLayout) container.findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);


    ListView hlvCategory = (ListView) rootView
            .findViewById(R.id.group_content);

    AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
    HomeJsonData hjd = new HomeJsonData(getActivity(), hlvCategory,
            mAdView, mDrawerList);

    hjd.execute();

    return rootView;
}

//SWIPE TO REFRESH
public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
        @Override public void run() {
            swipeLayout.setRefreshing(false);
        }
    }, 5000);
}

我有一个像这样的管道类

class ProductsSpider(scrapy.Spider):
    name = "products"
    allowed_domains = ["example.com"]
    start_urls = [
        'http://example.com/url'
    ]

    def parse(self, response):

但我想在parse_item函数中获取parse函数的响应参数而不将其设置为item对象的属性,是否可能

1 个答案:

答案 0 :(得分:2)

不,不可能。

响应不会转发到管道。您必须在项目中存储响应或使用一些外部存储来存储响应并在管道中获取它。第二种选择要好得多,并且避免了在项目中存储响应(例如,存储器问题)可能导致的许多问题。例如,您在解析回调中保存对某种形式的存储的响应,在项目字段中保存对此存储的引用,以及从管道中的存储中获取响应。

但这实际上取决于你要做的事情,spider middleware process_spider_output中有响应,所以也许你可以使用它而不是处理管道中的项目。