from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from manga.items import MangaItem
class MangaHere(BaseSpider):
name = "mangah"
allowed_domains = ["mangahere.com"]
start_urls = ["http://www.mangahere.com/seinen/"]
def parse(self,response):
hxs = HtmlXPathSelector(response)
sites = hxs.select('//ul/li/div')
items = []
for site in sites:
rating = site.select("p/span/text()").extract()
if rating > 4.5:
item = MangaItem()
item["title"] = site.select("div/a/text()").extract()
item["desc"] = site.select("p[2]/text()").extract()
item["link"] = site.select("div/a/@href").extract()
item["rate"] = site.select("p/span/text()").extract()
items.append(item)
return items
My goal is to crawl http://www.mangahere.com/seinen or anything on that site. I want to go through every page and collect books that are greater than a 4.5 rating. I started out as a basespider and tried copying and reading the scrapy tutorial but it pretty much went in over my head. I am here to ask what do i do to create my rules, and how. I also cant seem to get my condition to work, the code either only returns the very first item and stops regardless of condition, or grabs everything, again regardless of condition. I know its probably pretty messed up code but I am still struggling to learn. Feel free to touch up the code or offer other advice
Strictly speaking, this isn’t answering the question since my code uses a
BaseSpiderinstead of aCrawlSpider, but it does fulfil the OP’s requirement so…Points to note:
parsecallback, I search for a link with a class of “next” (there’s only one, so have a look to see which link it corresponds to), and then find its immediately preceding sibling. This gives me a handle on the total number of pages in the seinen category (currently 45).parse_itemcallback.ratingis a list and that its values are floats (which I should have realised on the basis that the condition is 4.5), the way to fix the error encountered is to loop through the list of ratings and cast each item to be a float.Anyway, have a look at the following code and see if it makes sense. In theory you should be able to easily extend this code to scrape multiple categories, though that is left as an exercise for the OP. 🙂