刚刚开始使用Scrapy,我希望能在正确的方向上有所作为。
我想从这里抓取数据:
https://www.sportstats.ca/display-results.xhtml?raceid=29360
这就是我到目前为止所知道的:
import scrapy
import re
class BlogSpider(scrapy.Spider):
name = 'sportstats'
start_urls = ['https://www.sportstats.ca/display-results.xhtml?raceid=29360']
def parse(self, response):
headings = []
results = []
tables = response.xpath('//table')
headings = list(tables[0].xpath('thead/tr/th/span/span/text()').extract())
rows = tables[0].xpath('tbody/tr[contains(@class, "ui-widget-content ui-datatable")]')
for row in rows:
result = []
tds = row.xpath('td')
for td in enumerate(tds):
if headings[td[0]].lower() == 'comp.':
content = None
elif headings[td[0]].lower() == 'view':
content = None
elif headings[td[0]].lower() == 'name':
content = td[1].xpath('span/a/text()').extract()[0]
else:
try:
content = td[1].xpath('span/text()').extract()[0]
except:
content = None
result.append(content)
results.append(result)
for result in results:
print(result)现在我需要转到下一个页面,我可以在浏览器中通过单击底部的“右箭头”来执行此操作,我相信这就是下面的li:
<li><a id="mainForm:j_idt369" href="#" class="ui-commandlink ui-widget fa fa-angle-right" onclick="PrimeFaces.ab({s:"mainForm:j_idt369",p:"mainForm",u:"mainForm:result_table mainForm:pageNav mainForm:eventAthleteDetailsDialog",onco:function(xhr,status,args){hideDetails('athlete-popup');showDetails('event-popup');scrollToTopOfElement('mainForm\\:result_table');;}});return false;"></a>我怎么才能让scrapy跟上它呢?
发布于 2016-05-13 08:21:41
如果你在没有javascript的浏览器中打开url,你将不能转到下一页。正如您在li标记中看到的,为了获得下一个页面,需要执行一些javascript。
为了解决这个问题,第一个选项通常是尝试识别由javascript生成的请求。在您的例子中,这应该很简单:只需分析java脚本代码,并在爬行器中使用python复制它。如果你能做到这一点,你可以从scrapy发送同样的请求。如果你不能做到这一点,下一个选择通常是使用一些带有javascript/浏览器仿真的包或类似的东西。像ScrapyJS或Scrapy + Selenium这样的东西。
发布于 2016-05-16 07:05:57
您将需要执行回调。从“下一页”按钮从xpath生成url。所以url = response.xpath(xpath to next_page_button),然后当你抓取完那个页面后,你将执行yield scrapy.Request(url, callback=self.parse_next_page)。最后,创建一个名为def parse_next_page(self, response):的新函数。
最后,最后要注意的是,如果它恰好是在Javascript中(即使您确定使用的是正确的xpath,也无法将其抓取),请查看我在将splash与scrapy https://github.com/Liamhanninen/Scrape结合使用一文中的参考资料
https://stackoverflow.com/questions/37189014
复制相似问题