Scrape query JavaScript with scrapy

Scrape query JavaScript with scrapy

本文关键字:scrapy with JavaScript query Scrape      更新时间:2023-09-26

我想在HTML正文或文本中转换查询javascript,但我不知道该怎么做。目标是 http://www.americandent.es/ficha-producto.php?id=19373,最后一个数字是可变的。

法典:

class AmericanSpider(scrapy.Spider):
    name = 'American2'
    start_urls = ['http://www.americandent.es/inicio/log-0']
    allowed_domains = ['americandent.es']
    def parse(self, response):
        return [FormRequest.from_response(response,
                    formdata={'username': 'name', 'password': 'secret'},
                    callback=self.after_login)]

    # continue scraping with authenticated session...
    def after_login(self, response):
    # check login succeed before going on
        if "authentication failed" in response.body:
            self.logger.error("Login failed")
            return
    # We've successfully authenticated, let's have some fun!
        else:
            print('LOGG!')
            for num in range(1,25000):
                yield  Request(url='http://www.americandent.es/ficha-producto.php?id='+str(num),
                    callback=self.parse_tastypage)

    def parse_tastypage(self, response):
        cont=0
        parsed_body = html.fromstring(response.text)
        item=StackItem()
        print(parsed_body)
        for sel in parsed_body.xpath('//*[@id="lightbox-home"]//text()'):
            #codigo producto = reference
            item['reference'] = sel.xpath('//*[@id="codigo_producto"]//text()').extract()
            item['name'] = sel.xpath('//*[@id="nombre_producto"]//text()').extract()
            #tarifa = price
            item['price'] = sel.xpath('//*[@id="lb-tarifas"]/div/div[2]/p/strong//text()').extract()
            item['stock'] = sel.xpath('//*[@id="stock_actual"]//text()').extract()
            item['imagen'] = sel.xpath('//*[@id="stock"]/img//@src').extract()
        return item

编辑:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <!--Stylesheets-->
    <!--<link href="http://www.americandent.es/css/main.css" rel="stylesheet" type="text/css" media="screen" charset="utf-8" />
    <link href="http://www.americandent.es/css/styles-registro.css" rel="stylesheet" type="text/css" media="screen" charset="utf-8" />
    <link href="http://www.americandent.es/css/../js/jquery.fancybox/jquery.fancybox-1.3.4.css" rel="stylesheet" type="text/css" media="screen" charset="utf-8" />
    -->
    <style type="text/css">
    .select_cont {
        background: url("http://www.americandent.es/images/bg_select_2.png") no-repeat scroll left -35px transparent;
        float: left;
        height: 29px;
        margin-bottom: 19px;
        outline: medium none;
        padding-left: 4px;
        position: relative;
    }
    .select_cont .eleccion {
        background: url("http://www.americandent.es/images/bg_select_2.png") no-repeat scroll right top transparent;
        cursor: default;
        font-size: 12px;
        font-weight: bold;
        height: 100%;
        line-height: 29px;
        margin-right: 18px;
        padding: 0 26px 0 6px;
        width: 256px;
    }
    .panel {
        display: none;
        left: 0;
        position: absolute;
        top: 31px;
        z-index: 10;
    }
    .panel ul {
        background-color: #E6E6E6;
        border-left: 1px solid #D8D5D5;
        border-right: 1px solid #D8D5D5;
        font-size: 12px;
        margin: 0;
        max-height: 21em;
        overflow: auto;
        padding: 0;
        width: 290px;
    }
    .panel li {
        cursor: default;
        height: 25px;
        line-height: 25px;
        padding: 0 12px;
    }
    .select_cont .eleccion { width:315px }
    .panel ul { width:349px }
    .panel .sup_panel {
        height:4px;
        background:url("http://www.americandent.es/images/sup_inf_panel2.png") no-repeat;
    }
    .panel .inf_panel {
        height:4px;
        background:url("http://www.americandent.es/images/sup_inf_panel2.png") no-repeat bottom;
    }   
        .capa_de_grupo {
            display:none;
        }
    </style>    
</head>
<body>    
    <div id="lightbox-home">
    <div id="mensaje">
            <h3>El producto ha sido a&ntilde;adido con &eacute;xito a tu carrito.</h3>
            <p>Podr&aacute;s proceder al pago de los productos a&ntilde;adidos mediante el acceso a 'Carrito' del men&uacute; principal de la web. Te invitamos a seguir navegando por nuestro cat&aacute;logo de productos. Gracias.</p>
            <img class="cerrar" src="http://www.americandent.es/images/close.png" alt="cerrar" title="cerrar" />
    </div>
    <div id="mensaje_error">
            <h3>No se ha a&ntilde;adido el producto a tu carrito.</h3>
            <p>Parece que ha habido un error al intentar a&ntilde;adir el producto a tu carrito. Por favor, int&eacute;ntalo de nuevo pasados unos minutos.</p>
            <img class="cerrar" src="http://www.americandent.es/images/close.png" alt="cerrar" title="cerrar" />
    </div>  
    <div id="mensaje_unidades_error">
            <h3>Ya has añadido las unidades a tu carrito.</h3>
            <p>Por favor, espera unos segundos para poder añadir más unidades.</p>
            <img class="cerrar" src="http://www.americandent.es/images/close.png" alt="cerrar" title="cerrar" />
    </div>
    <div id="mensaje_stock_error">
            <h3>No hay stock disponible.</h3>
            <p>No hay más unidades disponibles de este producto para añadir a tu carrito.</p>
            <img class="cerrar" src="http://www.americandent.es/images/close.png" alt="cerrar" title="cerrar" />
    </div>      
    <input type="hidden" id="id_producto" value="19373" />
    <input type="hidden" id="uds_en_carrito" value="0" />
    <div id="inline">
            <div id="stock">
                <img src="http://www.americandent.es/imagenes/productos/19373/th3_3+1reposicionesprimeandbond.jpg" alt="00  Prime & Bond Nt Promo Pack Doble (4x4,5ml.+ 2x2,5ml)" alt="" />
                                    <div id="lb-comprar" >
                        <p>COMPRAR </p>
            <div id="lb-unidades">
                            <form id="lb-formulario" action="" method="post">
                            <div class="clearUnidades">
                            <img src="http://www.americandent.es/images/bg_login-L.png" alt="" />
                <input class="inputNormal-lb" type="text" id="inputUnidades" value="1">
                            <img src="http://www.americandent.es/images/bg_login-R.png" alt="" />
                            </div>
                            <h3>unidades</h3>
                        <img src="http://www.americandent.es/images/ln_lightbox.png" alt="" />
                            <div class="clearAnadir">
                                <a class="buttonAnadir icoAn " href="#">
                                <span>A&ntilde;adir al carrito</span></a> 
                            </div>
                            <div id="anadir_loading"><img src="http://www.americandent.es/images/ajax-loader.gif" /></div>
                            </form>
            </div> <!--lb-unidades-->
                    </div>
                               <div id="ofVinculada">
                                         </div>
            </div>
            <div id="detalle">
                <div id="lb-descripcion">
                                <h1 id="nombre_producto">00  Prime & Bond Nt Promo Pack Doble (4x4,5ml.+ 2x2,5ml)</h1>
                <p>OBTURACION > ADHESIVOS</p>
                <p>Fabricante: DENTSPLY</p>
                <h4>C&oacute;digo: <span id="codigo_producto">02CA60667238</span></h4>
                                                                <p><strong>Stock:</strong> <span id="stock_actual">16</span> u</p>
                                <p><strong>Stock pr&oacute;ximo:</strong> <span id="stock_prevision">0</span>u <span id='fecha_stock'></span></p>
                                                <p style="max-height:130px;overflow:auto"><br />
                </p>
            </div>
                <div id="lb-tarifas">
                    <p><strong>TARIFAS </strong></p>                
                                            <div class="tarifas">
                                                                <div class="lb-linea"></div>
                                    <div class="lb-tarifaseuros"><p><strong>231,01&euro; </strong></p></div>
                                    <p>Su tarifa</p>
                                                            </div>
                </div>
            </div> <!--detalle-->
    </div>        
    </div>
<script type="text/javascript">
$(".select_cont").desplegable();
</script>
</body>

现在,我想盘点,名称,参考,描述等,但问题是获取网址 php 不需要任何内容,因为我认为它会进行查询

一种方法是加载网页并在加载后对其进行抓取。一种方法是使用启动服务器(启动库 https://github.com/scrapy-plugins/scrapy-splash),然后再次尝试抓取代码。 您需要在设置中添加 Splash 到中间件,并添加一个函数来传达爬虫本身内的 Splash 端点。

查看我的回购,逐步解释它 https://github.com/Liamhanninen/Scrape