【问题标题】:Pagination using scrapy使用scrapy进行分页
【发布时间】:2013-04-14 06:57:50
【问题描述】:

我正在尝试抓取此网站: http://www.aido.com/eshop/cl_2-c_189-p_185/stationery/pens.html

我可以获取此页面中的所有产品,但是如何在页面底部发出“查看更多”链接的请求?

到目前为止我的代码是:

rules = (
    Rule(SgmlLinkExtractor(restrict_xpaths='//li[@class="normalLeft"]/div/a',unique=True)),
    Rule(SgmlLinkExtractor(restrict_xpaths='//div[@id="topParentChilds"]/div/div[@class="clm2"]/a',unique=True)),
    Rule(SgmlLinkExtractor(restrict_xpaths='//p[@class="proHead"]/a',unique=True)),
    Rule(SgmlLinkExtractor(allow=('http://[^/]+/[^/]+/[^/]+/[^/]+$', ), deny=('/about-us/about-us/contact-us', './music.html',  ) ,unique=True),callback='parse_item'),
)

有什么帮助吗?

【问题讨论】:

    标签: python request web-scraping scrapy


    【解决方案1】:

    首先,您应该看一下这个线程,了解如何处理抓取 ajax 动态加载的内容: Can scrapy be used to scrape dynamic content from websites that are using AJAX?

    因此,单击“查看更多”按钮会触发 XHR 请求:

    http://www.aido.com/eshop/faces/tiles/category.jsp?q=&categoryID=189&catalogueID=2&parentCategoryID=185&viewType=grid&bnm=&atmSize=&format=&gender=&ageRange=&actor=&director=&author=&region=&compProductType=&compOperatingSystem=&compScreenSize=&compCpuSpeed=&compRam=&compGraphicProcessor=&compDedicatedGraphicMemory=&mobProductType=&mobOperatingSystem=&mobCameraMegapixels=&mobScreenSize=&mobProcessor=&mobRam=&mobInternalStorage=&elecProductType=&elecFeature=&elecPlaybackFormat=&elecOutput=&elecPlatform=&elecMegaPixels=&elecOpticalZoom=&elecCapacity=&elecDisplaySize=&narrowage=&color=&prc=&k1=&k2=&k3=&k4=&k5=&k6=&k7=&k8=&k9=&k10=&k11=&k12=&startPrize=&endPrize=&newArrival=&entityType=&entityId=&brandId=&brandCmsFlag=&boutiqueID=&nmt=&disc=&rat=&cts=empty&isBoutiqueSoldOut=undefined&sort=12&isAjax=true&hstart=24&targetDIV=searchResultDisplay
    

    返回接下来的 24 项中的 text/html。请注意这个hstart=24 参数:第一次点击“查看更多”它等于 24,第二次 - 48 等等。这应该是你的救命稻草。

    现在,您应该在您的蜘蛛中模拟这些请求。推荐的方法是实例化 scrapy 的 Request 对象,提供回调,您将在其中提取数据。

    希望对您有所帮助。

    【讨论】:

    • 这很有帮助,但是如何“实例化 scrapy 的请求对象”的示例会更有帮助。
    猜你喜欢
    • 2021-12-05
    • 2017-07-03
    • 2023-04-03
    • 1970-01-01
    • 2021-01-13
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多