抓取ajax动态网页java(林志强:我正在尝试使用scrapy+跟踪(图))

优采云 发布时间: 2022-01-11 10:08

  抓取ajax动态网页java(林志强:我正在尝试使用scrapy+跟踪(图))

  林志强:

  我正在尝试使用 scrapy 从网页中抓取产品信息。我的抓取页面是这样的:

  我尝试复制下一个按钮的 ajax 调用,但没有成功,所以我尝试使用 selenium。我可以在单独的脚本中运行 selenium 的 webdriver,但我不知道如何与 scrapy 集成。硒部分应该放在哪里?

  我的蜘蛛很标准,像这样:

  class ProductSpider(CrawlSpider):

name = "product_spider"

allowed_domains = ['example.com']

start_urls = ['http://example.com/shanghai']

rules = [

Rule(SgmlLinkExtractor(restrict_xpaths='//div[@id="productList"]//dl[@class="t2"]//dt'), callback='parse_product'),

]

def parse_product(self, response):

self.log("parsing product %s" %response.url, level=INFO)

hxs = HtmlXPathSelector(response)

# actual data follows

  任何想法都值得赞赏。谢谢!

  亚历克斯:

  这实际上取决于您需要如何抓取网站以及您想要获取的数据的方式和内容。

  这是一个示例,您可以使用 Scrapy+ 通过 Selenium 在 eBay 上跟踪分页:

  import scrapy

from selenium import webdriver

class ProductSpider(scrapy.Spider):

name = "product_spider"

allowed_domains = ['ebay.com']

start_urls = ['http://www.ebay.com/sch/i.html?_odkw=books&_osacat=0&_trksid=p2045573.m570.l1313.TR0.TRC0.Xpython&_nkw=python&_sacat=0&_from=R40']

def __init__(self):

self.driver = webdriver.Firefox()

def parse(self, response):

self.driver.get(response.url)

while True:

next = self.driver.find_element_by_xpath('//td[@class="pagn-next"]/a')

try:

next.click()

# get the data and write it to scrapy items

except:

break

self.driver.close()

  以下是“硒蜘蛛”的一些示例:

  除了必须与 Selenium 一起使用之外,还有另一种 Scrapy 的替代品。在某些情况下,使用 ScrapyJS 中间件足以处理页面的动态部分。实际使用示例:

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线