最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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)的評論 (共 條)

分享到微博請遵守國家法律
开封市| 通州市| 阿拉尔市| 汉源县| 大埔县| 延川县| 新民市| 建湖县| 滁州市| 安丘市| 苗栗市| 吉林市| 克什克腾旗| 尤溪县| 崇礼县| 丹寨县| 黎平县| 紫云| 河津市| 嵊州市| 肇东市| 荣昌县| 米脂县| 德钦县| 沈阳市| 和田市| 唐海县| 普兰店市| 博爱县| 黎川县| 盱眙县| 福清市| 岫岩| 巴南区| 东城区| 遵义县| 木兰县| 合肥市| 兴化市| 方山县| 铁力市|