Hi folks,
Today let us learn about getting the title and url for stack overflow questions.
So, for that we will be using Scrapy framework available in Python. And, also we will be creating a json file after the extraction.
from scrapy.item import Item, Field
class StackItem(Item):
# define the fields for your item here like:
# name = scrapy.Field()
title = Field()
url = Field()
from scrapy import Spider
from scrapy.selector import Selector
from stack.items import StackItem
class StackSpider(Spider):
name = "stack"
allowed_domains = ["stackoverflow.com"]
start_urls = [
"http://stackoverflow.com/questions?pagesize=50&sort=newest",
]
def parse(self, response):
questions = Selector(response).xpath('//div[@class="summary"]/h3')
for question in questions:
item = StackItem()
item['title'] = question.xpath('a[@class="question-hyperlink"]/text()').extract()[0]
item['url'] = question.xpath('a[@class="question-hyperlink"]/@href').extract()[0]
yield item
scrapy crawl stack -o questions.json
This was all about the stack overflow scraping. We can do more on this. Like, storing data in database. And, also extracting page wise data. Explore all these, and, let me know if you run into any problems through comments.
Also, if you liked this article, please upvote it!
Thanks, see you all in next post!