国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

22. Scrapy 框架-案例實現(xiàn)

2020-07-02 14:11 作者:自學(xué)Python的小姐姐呀  | 我要投稿

爬取小說

spider

import scrapy
from xiaoshuo.items import XiaoshuoItem


class XiaoshuoSpiderSpider(scrapy.Spider):
? ?name = 'xiaoshuo_spider'
? ?allowed_domains = ['zy200.com']
? ?url = 'http://www.zy200.com/5/5943/'
? ?start_urls = [url + '11667352.html']

? ?def parse(self, response):
? ? ? ?info = response.xpath("/html/body/div[@id='content']/text()").extract()
? ? ? ?href = response.xpath("//div[@class='zfootbar']/a[3]/@href").extract_first()
? ? ? ?xs_item = XiaoshuoItem()
? ? ? ?xs_item['content'] = info
? ? ? ?yield xs_item

? ? ? ?if href != 'index.html':
? ? ? ? ? ?new_url = self.url + href
? ? ? ? ? ?yield scrapy.Request(new_url, callback=self.parse)

items

import scrapy


class XiaoshuoItem(scrapy.Item):
? ?# define the fields for your item here like:
? ?content = scrapy.Field()
? ?href = scrapy.Field()

pipeline

class XiaoshuoPipeline(object):
? ?def __init__(self):
? ? ? ?self.filename = open("dp1.txt", "w", encoding="utf-8")

? ?def process_item(self, item, spider):
? ? ? ?content = item["title"] + item["content"] + '\n'
? ? ? ?self.filename.write(content)
? ? ? ?self.filename.flush()
? ? ? ?return item

? ?def close_spider(self, spider):
? ? ? ?self.filename.close()


22. Scrapy 框架-案例實現(xiàn)的評論 (共 條)

分享到微博請遵守國家法律
河北区| 英山县| 万山特区| 乾安县| 乐平市| 广元市| 五华县| 尼玛县| 喜德县| 许昌县| 苍梧县| 泸水县| 孝昌县| 湖南省| 册亨县| 宜黄县| 晋州市| 孟连| 江油市| 布尔津县| 烟台市| 于田县| 南靖县| 舟曲县| 华安县| 星子县| 甘泉县| 仙游县| 庆阳市| 商河县| 龙里县| 郸城县| 禹城市| 富锦市| 凤凰县| 桦南县| 社会| 龙门县| 平顺县| 潼南县| 台北县|