Home > Enterprise >  Provide None scraping address
Provide None scraping address

Time:06-24

I am scraping the address but they will provide me none these is a page link https://www.baroulconstanta.ro/tabloul-avocatilor/avocati-definitivi/

import scrapy
from scrapy.http import Request


class TestSpider(scrapy.Spider):
    name = 'test'
    start_urls=["https://www.baroulconstanta.ro/tabloul-avocatilor/avocati-definitivi/"]
    
    
    
    
    def parse(self, response):
        address=response.xpath("//div[@class='col-md-12']//p[1]//text()[2]").get()
        print(address)
        

CodePudding user response:

You are getting empty output because data is generating from external via ajax request as post method. So you have to use API url

from scrapy.crawler import CrawlerProcess
from scrapy.http import Request

class TestSpider(scrapy.Spider):
    name = 'test'
    
    def start_requests(self):
        yield Request(
            url= "https://www.ifep.ro/Justice/Lawyers/LawyersPanel.aspx?CompanyId=1115&CurrentPanel=Definitivi&HideHeader=1&HideFooter=1&HideFilters=1",
            callback=self.parse,
            method= "POST",
            headers= {
                'content-type': 'application/x-www-form-urlencoded',
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
                'x-microsoftajax':'Delta=true',
                'x-requested-with': 'XMLHttpRequest'
            }
        )

    def parse(self, response):
        for address in response.xpath('//*[@]'):
            print(''.join([x.get() for x in address.xpath('.//p/text()[2]')]).replace('\r\n\t','').strip())

if __name__ == "__main__":
    process =CrawlerProcess()
    process.crawl(TestSpider)
    process.start()

Output:

Sediu principal în Baroul Constanţa, adresă: NAVODARI STR .CONSTANTEI NR. 15 BL, E 1, SC. A, APRT.14
Sediu principal în Baroul Constanţa, adresă: CONSTANTA STR. BOGDAN VODA NR. 63 A
Sediu principal în Baroul Constanţa, adresă: NAVODARI CARTIER MAMAIA SAT STR. M 10 , NR. 26  CSediu principal în Baroul Constanţa, adresă: CONSTANŢA BD. FERDINAND NR. 26
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. VASILE PARVAN NR. 5
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. MIRCEA NR. 181, BL. M S 3 B, SC. B, AP. 38
Sediu principal în Baroul Constanţa, adresă: CONSTANTA STR. ILEANA COSANZEANA NR. 18
Sediu principal în Baroul Constanţa, adresă: CONSTANTA STR.PESCARILOR NR. 35 BL. fz 14, APRT. 
51
Sediu principal în Baroul Constanţa, adresă: CONSTANTA BD. TOMIS NR. 46 , ET.2 ,CAMERA 29     
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. NICOLAE MILESCU NR. 57 BL. P N 2 , SC.A ,APRT. 42
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA BD.FERDINAND NR. 87, BL. A 4, SC. A. APRT. 1
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. CĂLĂRAŞI NR. 46 ET. 1
Sediu principal în Baroul Constanţa, adresă: CONSTANTA STR. CONSTANTIN BOBESCU NR. 23, BL C, AP. 6
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. DIMITRIE BOLINTINEANU NR. 32 A    
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. NEHOIULUI NR. 37

CodePudding user response:

You are not able to extract the data, because the part of the page that you are looking for is in the iframe (page inside the page). You have two options: you can manually look for the URL of the iframe and parse it in the code instead OR you can find this URL programmatically and again parse it in the code.

Here is how you can do the second approach (please bear with me, this is the first time I am using scrapy, so I am most likely butchering it, and there are more appropriate ways of doing it):

import scrapy

class TestSpider(scrapy.Spider):
    name = 'test'
    start_urls=["https://www.baroulconstanta.ro/tabloul-avocatilor/avocati-definitivi/"]
    
    def parse(self, response):
        frame = response.css('iframe::attr(src)').get()
        yield scrapy.Request(url=frame, callback=self.parse_frame, meta=response.meta)

    def parse_frame(self, response):
        addresses = response.xpath("//div[@class='col-md-12']//p[1]//text()[2]")

        for addr in addresses:
            print(addr.get().strip())

produces:

Sediu principal în Baroul Constanţa, adresă: NAVODARI STR .CONSTANTEI NR. 15 BL, E 1, SC. A, APRT.14
Sediu principal în Baroul Constanţa, adresă: CONSTANTA STR. BOGDAN VODA NR. 63 A
Sediu principal în Baroul Constanţa, adresă: NAVODARI CARTIER MAMAIA SAT STR. M 10 , NR. 26  C
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA BD. FERDINAND NR. 26
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. VASILE PARVAN NR. 5
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. MIRCEA NR. 181, BL. M S 3 B, SC. B, AP. 38
Sediu principal în Baroul Constanţa, adresă: CONSTANTA STR. ILEANA COSANZEANA NR. 18
Sediu principal în Baroul Constanţa, adresă: CONSTANTA STR.PESCARILOR NR. 35 BL. fz 14, APRT. 51
Sediu principal în Baroul Constanţa, adresă: CONSTANTA BD. TOMIS NR. 46 , ET.2 ,CAMERA 29
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. NICOLAE MILESCU NR. 57 BL. P N 2 , SC.A ,APRT. 42
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA BD.FERDINAND NR. 87, BL. A 4, SC. A. APRT. 1
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. CĂLĂRAŞI NR. 46 ET. 1
Sediu principal în Baroul Constanţa, adresă: CONSTANTA STR. CONSTANTIN BOBESCU NR. 23, BL C, AP. 6
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. DIMITRIE BOLINTINEANU NR. 32 A
Sediu principal în Baroul Constanţa, adresă: CONSTANŢA STR. NEHOIULUI NR. 37
  • Related