Ben Davidow
Ben Davidow

Reputation: 1215

502 Error with Scrapy Script

Scraping newbie here. I'm using Scrapy to get a bunch of data from a single site. When I run the script it works fine for a few minutes but then slows down, just about stops and continually throws the following pair of errors with different URLs it is trying to scrape:

2013-07-20 14:15:17-0700 [billboard_spider] DEBUG: Retrying <GET http://www.billboard.com/charts/1981-01-17/hot-100> (failed 1 times): Getting http://www.billboard.com/charts/1981-01-17/hot-100 took longer than 180 seconds.

2013-07-20 14:16:56-0700 [billboard_spider] DEBUG: Crawled (502) <GET http://www.billboard.com/charts/1981-01-17/hot-100> (referer: None) 

The above error piles up with different URLs and I'm not sure what's causing it...

Here's the script:

import datetime
from scrapy.item import Item, Field
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector


class BillBoardItem(Item):
    date = Field()
    song = Field()
    artist = Field()


BASE_URL = "http://www.billboard.com/charts/%s/hot-100"


class BillBoardSpider(BaseSpider):
    name = "billboard_spider"
    allowed_domains = ["billboard.com"]

    def __init__(self):
        date = datetime.date(year=1975, month=12, day=27)

        self.start_urls = []
        while True:
            if date.year >= 2013:
                break

            self.start_urls.append(BASE_URL % date.strftime('%Y-%m-%d'))
            date += datetime.timedelta(days=7)

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        date = hxs.select('//span[@class="chart_date"]/text()').extract()[0]

        songs = hxs.select('//div[@class="listing chart_listing"]/article')
        item = BillBoardItem()
        item['date'] = date
        for song in songs:
            try:
                track = song.select('.//header/h1/text()').extract()[0]
                track = track.rstrip()
                item['song'] = track
                item['artist'] = song.select('.//header/p[@class="chart_info"]/a/text()').extract()[0]
                break
            except:
                continue 

         yield item

Upvotes: 5

Views: 4843

Answers (1)

alecxe
alecxe

Reputation: 474003

The spider works for me and scrapes the data without any problems. So, as @Tiago assumed, you were banned.

Read how to avoid getting banned in the future and tweak your scrapy setting appropriately. I'd start with trying to increase DOWNLOAD_DELAY and rotate your IPs.

Also, consider switching to using real automated browser, like selenium.

Also, see if you can get the date from RSS XML feeds: http://www.billboard.com/rss.

Hope that helps.

Upvotes: 5

Related Questions