五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

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

分享到微博請遵守國家法律
双峰县| 宜阳县| 荥阳市| 阿克| 友谊县| 盱眙县| 泰来县| 石门县| 九江市| 从江县| 云龙县| 鄄城县| 四子王旗| 安庆市| 榆林市| 枣庄市| 五寨县| 盐亭县| 图们市| 濉溪县| 兴业县| 麟游县| 宁德市| 礼泉县| 穆棱市| 兰考县| 伊吾县| 文登市| 五华县| 沙河市| 大宁县| 内丘县| 阜新市| 崇仁县| 定襄县| 西宁市| 自贡市| 浑源县| 铜陵市| 都兰县| 城市|