Search code examples
pythonweb-scrapingscrapyweb-mining

How to get text and href value in anchor tag with scrapy, xpath, python


I have a HTML file like this:

<div ckass="jokes-nav">
  <ul>
    <li><a href="http://link_1">Link 1</a></li>
    <li><a href="http://link_2">Link 2</a></li>
  </ul>
</div>

In the folder spiders, I have a file jokes.py like this:

import scrapy
from demo_project.items import JokeItem
from scrapy.loader import ItemLoader

class JokesSpider(scrapy.Spider):
    name = 'jokes'

    start_urls = [
        'http://www.laughfactory.com/jokes/'
    ]

    def parse(self, response):
        for joke in response.xpath("//div[@class='jokes-nav']/ul"):
            l = ItemLoader(item = JokeItem(), selector = joke)
            l.add_xpath('joke_title', ".//li/a/text()")

            """ yield {
                'joke_text': joke.xpath(".//div[@class='joke-text']/p").extract_first()
            } """

            yield l.load_item()

and I call the class JokesSpider in my main.py (this file is at root), and this is my code

from scrapy.crawler import CrawlerProcess
from demo_project.spiders.jokes import JokesSpider

process = CrawlerProcess(settings={
    "FEEDS": {
        "items.json": {"format": "json"},
    },
})

process.crawl(JokesSpider)
process.start() # the script will block here until the crawling is finished

I want to write data to items.json, but when I run this code, items.json does not contain anything in it, how can I solve this problem. Thank you very much


Solution

  • You can set FEED_FORMAT and FEED_URI settings to save data in a json file.

    process = CrawlerProcess(settings={
        'FEED_FORMAT': 'json',
        'FEED_URI': 'items.json'
    })