问题描述
我是scrapy的新手,想在excel中写入数据。我知道我编写数据框的方式。我将只能从一页中获取数据。但我很困惑,我必须定义数据框才能将所有数据写入 excel。
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider,Rule
import pandas as pd
class MonarkSpider(CrawlSpider):
name = 'monark'
allowed_domains = ['monark.com.pk']
start_urls = ['https://monark.com.pk/collections/t-shirts']
#Extracting links
rules = (Rule(LinkExtractor(restrict_xpaths=('//h4[@class="h6 m-0 ff-main"]/a')),callback='parse_item',follow=True),)
def parse_item(self,response):
item = {
'Product_Name' : response.xpath('//h1[@class="h3 m-0"]/text()').get(),'Price' : response.xpath('//span[@class="price price--sale"]/span[2]/text()').get(),'Description' : response.xpath('//div[@class="tabs__content rte overflow-hidden"]/p/text()').get()
}
# writing data
df = pd.DataFrame([item],columns=['Product_Name','Price','Description'])
df.to_excel('Monark.xlsx',sheet_name='Monark_DATA',index=False,header=True)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)