I'm scraping review from moocs likes this one
From there I'm getting all the course details, 5 items and another 6 items from each review itself.
This is the code I have for the course details:
def parse_reviews(self, response):
l = ItemLoader(item=MoocsItem(), response=response)
l.add_xpath('course_title', '//*[@class="course-header-ng__main-info__name__title"]//text()')
l.add_xpath('course_description', '//*[@class="course-info__description"]//p/text()')
l.add_xpath('course_instructors', '//*[@class="course-info__instructors__names"]//text()')
l.add_xpath('course_key_concepts', '//*[@class="key-concepts__labels"]//text()')
l.add_value('course_link', response.url)
return l.load_item()
Now I want to include the review details, another 5 items for each review. Since the course data is common for all the reviews I want to store it in a different file and use course name/id to relate the data afterward.
This is the code I have for the review's items:
for review in response.xpath('//*[@class="review-body"]'):
review_body = review.xpath('.//div[@class="review-body__content"]//text()').extract()
course_stage = review.xpath('.//*[@class="review-body-info__course-stage--completed"]//text()').extract()
user_name = review.xpath('.//*[@class="review-body__username"]//text()').extract()
review_date = review.xpath('.//*[@itemprop="datePublished"]/@datetime').extract()
score = review.xpath('.//*[@class="sr-only"]//text()').extract()
I tried to work with a temporary solution, returning all the items for each case but is not working either:
def parse_reviews(self, response):
#print response.body
l = ItemLoader(item=MoocsItem(), response=response)
#l = MyItemLoader(selector=response)
l.add_xpath('course_title', '//*[@class="course-header-ng__main-info__name__title"]//text()')
l.add_xpath('course_description', '//*[@class="course-info__description"]//p/text()')
l.add_xpath('course_instructors', '//*[@class="course-info__instructors__names"]//text()')
l.add_xpath('course_key_concepts', '//*[@class="key-concepts__labels"]//text()')
l.add_value('course_link', response.url)
for review in response.xpath('//*[@class="review-body"]'):
l.add_xpath('review_body', './/div[@class="review-body__content"]//text()')
l.add_xpath('course_stage', './/*[@class="review-body-info__course-stage--completed"]//text()')
l.add_xpath('user_name', './/*[@class="review-body__username"]//text()')
l.add_xpath('review_date', './/*[@itemprop="datePublished"]/@datetime')
l.add_xpath('score', './/*[@class="sr-only"]//text()')
yield l.load_item()
The output file for that script is corrupted, cells are displaced and the size of the fields is not correct.
EDIT: I want to have two files at the output:
The first one containing:
course_title,course_description,course_instructors,course_key_concepts,course_link
And the second one with:
course_title,review_body,course_stage,user_name,review_date,score