为每个

2024-04-19 00:34:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图刮去那个页面“https://myanimelist.net/anime.php?letter=A”,我找到了我想要的信息,但我想为每一行获取i并删除//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()}

而且,我甚至不确定是否能看完这一页上的每一部动画。 如果有人也能给我展示一个css方法,那就太好了(为了学习)


Tags: textinhttps信息fornetextract页面
1条回答
网友
1楼 · 发布于 2024-04-19 00:34:21

如前所述,我只是提供了一些数据点的CSS示例,而其他的则留给自己去探索:

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)

更多信息:https://www.w3schools.com/cssref/css_selectors.asp

相关问题 更多 >