Question

I am trying to get text from a extracted link that i extracted from the "main page"

this crawler is suposed to get all te links from the "main page" and then crawl all these links and get the text out of it.

I hope you understand what i mean, if not ask me then i try to explain more detailed

EDIT :

So i read a bit more about scrapy and found this page https://scrapy.readthedocs.org/en/latest/topics/spiders.html?highlight=crawlspider#crawling-rules i readed the "SitemapSpider" and i gues this can be the spider that i need to use for this issue

The Spider file *EDITED*

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

from craigslist_randstad.items import CraigslistRandstadItem

class MySpider(BaseSpider):
    name = "craig"
    allowed_domains = ["randstad.nl"]
    start_urls = ["http://www.randstad.nl/mwp2/faces/baanZoeken?afstand=10&pagina=1&_adf.ctrl-state=sb0riset7_4&sc=0&filters=vakgebied!5626&_afrLoop="]

def parse(self, response):
    sel = Selector(response)
    link = 

    for link in sel.xpath("//div[@class='search-result af_panelGroupLayout']"):
        link = sel.xpath(".//a[contains(@class, 'outer-read-more-link')]/@href").extract()
        yield Request(link, callback=self.parse)

    print len(link)


    text_list = sel.xpath('//div[@id="basePage:page:twoColumn:r2:0:functieOmschrijvingPanel::content"]/text()').extract()
    if text_list:
        text = '\n'.join(text_list)
        item = CraigItem()
        item['link'] = response.url
        item['title'] = text
        yield item

The Items File

from scrapy.item import Item, Field

class CraigslistRandstadItem(Item):
  title = Field()
  link = Field()
Was it helpful?

Solution 2

Guy gave you a good explanation, and here is an example to extract some data that can guide you to more complex scrapping:

First, create the project and access to its directory:

scrapy startproject craig && cd craig

Second, edit the file items.py:

from scrapy.item import Item, Field

class CraigItem(Item):
    title = Field()
    link = Field()

Third, create the spider. You can do this simple task with the most basic one:

scrapy genspider -t basic craig_spider 'randstad.nl'

Fourth, edit it, spiders/craig_spider.py:

from scrapy.spider import BaseSpider
from scrapy.selector import Selector
from craig.items import CraigItem
from scrapy.http import Request

class CraigSpiderSpider(BaseSpider):
    name = "craig_spider"
    allowed_domains = ["randstad.nl"]
    start_urls = ( 
        "http://www.randstad.nl/mwp2/faces/baanZoeken?afstand=10&pagina=1&_adf.ctrl-state=sb0riset7_4&sc=0&filters=vakgebied!5626&_afrLoop=",
        )   

    def parse(self, response):
        sel = Selector(response)

        for link in sel.xpath("//a[contains(@class, 'outer-read-more-link')]/@href").extract():
            yield Request(link, callback=self.parse)

        text_list = sel.xpath('//div[@id="basePage:page:twoColumn:r2:0:functieOmschrijvingPanel::content"]/text()').extract()
        if text_list:
            text = '\n'.join(text_list)
            item = CraigItem()
            item['link'] = response.url
            item['title'] = text
            yield item

Note that doing

links = hxs.select("//div[@class='search-result af_panelGroupLayout']")

to extract links and later

links.select("//a[contains(@class, 'outer-read-more-link')]/@href")

has little sense because //a[...] will search all anchors from the root element and not from the links subtree so I omit the first search here.

Note also that I do two different xpath searches, one that returns (yield) links to parse again (callback) and another one in the information pages to extract the whole text, create the item and return it.

Run it like:

scrapy crawl craig_spider -o craig.json -t json

That will create a text file with json format, like the following:

[{"link": "http://www.randstad.nl/vacatures/1486457/klantadviseur-glasvezel-tijdelijk-%28m-v%29", "title": "Wil je per direct aan de slag in een afwisselend project?  Heb jij ervaring in de glasvezel- of ICT-branche? Werk jij graag in een hectische, dynamische omgeving?\nDan hebben wij een leuke tijdelijke baan voor jou! \nDeze snelgroeiende en onafhankelijke serviceprovider levert een breed scala aan diensten zoals internet, televisie en telefonie op het glasvezelnetwerk . \nIn deze veelzijdige functie ben je verantwoordelijk voor een zo positief mogelijke klantbeleving. Je zorgt voor een proactieve, deskundige advisering aan klanten over diensten en de levering ervan. Je bent verantwoordelijk voor de Backoffice Support van het Klant Contact Center, je denkt actief mee over verbeteringen in communicatie, applicaties en de manier van werken binnen het bedrijf. Je ziet kansen in alle dagelijkse handelingen om de commercie te verhogen op de afdeling.\nWat ga je doen?\nBeantwoorden van tweede- en derdelijns vragen van klanten en externe partners, \nBewaken van procedures voor correcte administratieve afhandeling,\nVerwerken van orders en uitsturen hiervan per post en e-mail,\nBijdragen aan optimaliseren primaire klantprocessen.\nWie ben jij?\nJe hebt een opleiding afgerond op MBO+/HBO niveau, of bent studerend aan HBO of WO,\nMinimaal 1 jaar inbound contactcenterervaring,\nJe hebt een perfecte beheersing van de Nederlandse taal in woord en geschrift,\nErvaring met het Office pakket,\nWerkervaring in glasvezel- en/of ICT-branche is een grote pr\u00e9\nVerder ben je:\nBetrokken, betrouwbaar, transparant en innovatief,\nZelfstandig,\nKlantgericht en commercieel,\nJe vindt het leuk om klanten te enthousiasmeren,\nJe bent een oplossingsgerichte teamspeler en je hebt affiniteit met ICT.\nZie jij jezelf als de perfecte kandidaat voor deze baan? Dan horen we graag vandaag nog van je! Reageer direct via de solliciteer button met je Cv en vertel ons waarom jij de geschikte kandidaat bent voor deze functie!"},
{"link": "http://www.randstad.nl/vacatures/1360086/embedded-software-design-engineer-electric-controls-%28m-v%29", "title": "Ben jij degene die nieuwe dingen bedenkt? Doe je dat het liefst in een team van specialisten? En vind je het leuk bezig te zijn in het werkveld van elektrische installaties voor (railgebonden) voertuigen? lees dan snel verder...\nWat ga jij doen?\nOpstellen van systeem-, product- en module specificaties conform bestek, normen en voorschriften\nHet opstellen van softwarespecificaties en het ontwerp van de software\nFormuleren en uitvoeren van testplannen voor afname beproevingen\nRisicomanagement door toepassing van FMEA en FTA\nOpstellen van ontwerp- en gebruikers documentatie\nRapporteren aan de projectleider\n \nWat verwachten wij van jou?\nMinimaal een HBO-opleiding (Bij voorkeur WO), richting elektrotechniek, embedded systems of gelijkwaardig vakgebied\nRelevante ervaring met het toepassen van normen en richtlijnen voor kwaliteit en veiligheid\nUitstekende communicatieve vaardigheden\nSterk ontwikkeld analytisch denkvermogen\nUitstekende beheersing van de Engels en Nederlandse taal is noodzakelijk\nKennis van en ervaring met elektrische installaties voor railgebonden voertuigen\nKennis van en ervaring met CAN, MVB, C/C++, .NET/C#, FPGA, real-time OS, TCMS, CMMI, EN61508 SIL3, EN50128, FMEA en Matlab/Simulink\nHerken jij jezelf in dit profiel en heb jeinmiddels een aantal jaar werkervaring in jouw vakgebied? Ben jij toe aan de volgende stap in je carriere reageer dan snel via 06-15087716 of mail bas.kooter.rnl@nl.randstad.com"},
...
]

Final note: This example is done with the 0.20 version of . It changes the class used to extract data. Now it's called Selector. You will have to translate it for a lower version, but it's easy reading the docs.

OTHER TIPS

starting from the default parse() function, every spider function can yield a combination of some Item/s and/or Request/s, note that if you want to return multiple items/requests you'll have to yield and not return

scrapy is then responsible for sending items to pipelines and call those Requsts

Note the callback argument that should be given when creating a request, this is the function that scrapy will call with the response.

last, scrapy keeps track of the requests and will make sure they're not been called more than once unless you'll set dont_filter=True on the request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top