python
  • selenium
  • scrapy
  • 2013-04-21 65 views 1 likes 
    1

    我想使用Scrapy来解析一个相对简单的网页集。主页上有一堆看起来像链接:使用Selenium Python绑定的JavaScript链接

    <a name='LINK1$17' id='LINK1$17' tabindex='145' href="javascript:hAction_win0(document.win0,'LINK1$17', 0, 0, 'International Relations', false, true);" class='SSSAZLINK'>International Relations</a> 
    

    通过点击该链接负载高达第二页上其中一些我刮出现的细节。我确实需要从第一页开始,因为它可以作为我抓取的所有这些东西的索引。我如何使用硒来运行该JavaScript操作?我试过了:

    import webdriver 
    driver = webdriver.Firefox() 
    driver.execute_script("javascript:hAction_win0(document.win0,'LINK1$17', 0, 0, 'International Relations', false, true);") 
    

    这没有奏效。有没有简单的方法来“点击”链接并得到什么?

    +1

    你想在这里使用硒只是点击链接,对不对?然后,如果第二页由ajax XHR请求加载 - 看看[这个线程](http://stackoverflow.com/questions/8550114/can-scrapy-be-used-to-scrape-dynamic-content-从-网站 - 即-被-使用的Ajax?LQ = 1)。 – alecxe 2013-04-22 07:09:13

    +1

    因此,基本上你应该使用浏览器开发工具来查看当你点击链接时要向服务器发送什么请求。然后,在Scrapy的[请求](http://doc.scrapy.org/en/latest/topics/request-response.html)的帮助下,您应该在抓取工具中模拟它。 – alecxe 2013-04-22 07:11:31

    回答

    0

    原来我使用的是正确的功能。以下调用工作:

    driver.execute_script("hAction_win0(document.win0,'LINK1$17', 0, 0, 'International Relations', false, true);") 
    

    我刚刚在开始时删除了“javascript:”。

    相关问题