Search code examples
pythonscrapyscrapy-shell

Scrapy xpath selector for each row


i'm trying to scrape that page "https://myanimelist.net/anime.php?letter=A" , i found information that i want but i would like to got i for every row and remove //n /n

for anime in tree.xpath('//*[@id="content"]/div[5]/table//tr'): 
data = {"title" : anime.xpath("//strong//text()").extract(),
        "synopsis" :  anime.xpath("//td[2]//text()").extract(),
        "type_" :  anime.xpath("//td[3]//text()").extract(),
        "episodes" :  anime.xpath("//td[4]//text()").extract(),
        "score" :  anime.xpath("//td[5]//text()").extract()}

moreover, i'm even don't sure to catch every anime present on page. If someone can show me a css method too it would be great (in purpose to learn)


Solution

  • As asked, I'm just providing CSS examples of a few datapoints, while leaving the others for yourself to explore:

    In [1]: fetch('https://myanimelist.net/anime.php?letter=A')
    2018-11-06 23:15:40 [scrapy.core.engine] INFO: Spider opened
    2018-11-06 23:15:41 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://myanimelist.net/anime.php?letter=A> (referer: None)
    
    In [2]: for tr_sel in response.css('div.js-categories-seasonal tr ~ tr'):
       ...:     sample_data = {
       ...:         'title': tr_sel.css('a[id] strong::text').extract_first(),
       ...:         'type': tr_sel.css('td:nth-child(3)::text').extract_first(),
       ...:     }
       ...:     print(sample_data)
    

    Some further info: https://www.w3schools.com/cssref/css_selectors.asp