scrapy分页抓取网页(【平安二号?百日攻坚】我该)

优采云 发布时间: 2022-01-29 01:09

  scrapy分页抓取网页(【平安二号?百日攻坚】我该)

  【问题描述】:

  我有一个爬虫爬虫,它爬取一个 网站,它通过页面上的 javascript 重新加载内容。要跳转到下一页进行抓取,我一直在使用 Selenium 单击 网站 顶部的月份链接。

  问题是,即使我的代码按预期通过每个链接,蜘蛛只是抓取第一个月(9 月)的月份数据并返回这些重复项。

  我该如何解决这个问题?

  from selenium import webdriver

class GigsInScotlandMain(InitSpider):

name = 'gigsinscotlandmain'

allowed_domains = ["gigsinscotland.com"]

start_urls = ["http://www.gigsinscotland.com"]

def __init__(self):

InitSpider.__init__(self)

self.br = webdriver.Firefox()

def parse(self, response):

hxs = HtmlXPathSelector(response)

self.br.get(response.url)

time.sleep(2.5)

# Get the string for each month on the page.

months = hxs.select("//ul[@id='gigsMonths']/li/a/text()").extract()

for month in months:

link = self.br.find_element_by_link_text(month)

link.click()

time.sleep(5)

# Get all the divs containing info to be scraped.

listitems = hxs.select("//div[@class='listItem']")

for listitem in listitems:

item = GigsInScotlandMainItem()

item['artist'] = listitem.select("div[contains(@class, 'artistBlock')]/div[@class='artistdiv']/span[@class='artistname']/a/text()").extract()

#

# Get other data ...

#

yield item

  【问题讨论】:

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线