'Scrapy with Python in Visual Studio Code
Hello to all the community! I'm training in Python (DAW + Web Scraping). I am currently trying to run my code but, the problem I "see" is that the .csv comes out blank. The code works (if I'm not mistaken) but the problem is with the .csv. Thank you in advance for any help you can give me.
Thanks, Cristian.
from scrapy.item import Field
from scrapy.item import Item
from scrapy.spiders import Spider
from scrapy.selector import Selector
from scrapy.loader import ItemLoader
class Pregunta(Item):
id = Field()
pregunta = Field()
descripcion = Field()
class StackoverflowSpider(Spider):
name = "MiPrimerSpider"
custom_settings = {
'USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
url_semilla = ['https://stackoverflow.com/questions']
def parse(self, response):
selector = Selector(response)
preguntas = selector.xpath('//div[@class="s-post-summary js-post-summary"]')
i = 0
for pregunta in preguntas:
item = ItemLoader(Pregunta(), pregunta)
item.add_xpath('pregunta', './/h3/a/text()')
item.add_xpath('descripcion', './/div[@class="s-post-summary--content-excerpt"]/text()')
item.add_value('id', i)
i += 1
yield item.load_item()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
