Issue
I tried to use Scrapy to get article body from news site.
import scrapy
import sys
import json
class ReutersPage(scrapy.Spider):
name = "reutersPage"
start_urls = [
'https://www.reuters.com/article/chile-sqm-stocks/lithium-miner-sqm-shares-up-2-7-pct-chile-court-clears-way-for-tianqi-stake-purchase-idUSC0N1OX01C'
]
def parse(self, response):
articleBody = response.css('div.StandardArticleBody_body::text').extract_first()
print('######## Article body ##########')
print(articleBody)
yield {
'body': articleBody
}
I try to get text in div StandardArticleBody_body but always get None value.
The output is
2018-10-26 14:23:44 [scrapy.core.engine] INFO: Spider opened
2018-10-26 14:23:44 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-10-26 14:23:44 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2018-10-26 14:23:44 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.reuters.com/robots.txt> (referer: None)
2018-10-26 14:23:44 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.reuters.com/article/chile-sqm-stocks/lithium-miner-sqm-shares-up-2-7-pct-chile-court-clears-way-for-tianqi-stake-purchase-idUSC0N1OX01C> (referer: None)
######## Parse article ##########
######## Article body ##########
None
2018-10-26 14:23:45 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.reuters.com/article/chile-sqm-stocks/lithium-miner-sqm-shares-up-2-7-pct-chile-court-clears-way-for-tianqi-stake-purchase-idUSC0N1OX01C>
{'body': None}
2018-10-26 14:23:45 [scrapy.core.engine] INFO: Closing spider (finished)
Solution
There isn't any text belonging directly to the div
you're selecting, but to it's descendants. A space between the selector path and the ::
will get text
of all descendants, not just the text of the node you've selected.
Try this
articleBody = response.css('div.StandardArticleBody_body ::text').extract_first()
So that you're getting all the text of the div
's descendants.
Answered By - pwinz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.