前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python唐诗生成器

Python唐诗生成器

作者头像
葆宁
发布2022-01-06 08:47:07
2520
发布2022-01-06 08:47:07
举报
文章被收录于专栏:FREE SOLO
代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Date    : 2019/08/20 09:44
@Author  :wuyupku
@File    : shigeSpider.py
'''
from utils import MySpider, MongoBase
from datetime import date
from lxml import etree
import sys

class shigeSpider():
    def __init__(self):
        self.db = MongoBase()
        self.spider = MySpider()

    def download(self, url):
        self.domain = url.split('/')[2]
        data = self.spider.get(url)
        if data:
            self.parse(data)

    def parse(self, data):
        response = etree.HTML(data)
        for row in response.xpath('//div[@class="left"]/div[@class="sons"]'):
            title = row.xpath('div[@class="cont"]/p/a/b/text()')[0] if row.xpath('div[@class="cont"]/p/a/b/text()') else ''
            dynasty = row.xpath('div[@class="cont"]/p[@class="source"]//text()')[0] if row.xpath('div[@class="cont"]/p[@class="source"]//text()') else ''
            author = row.xpath('div[@class="cont"]/p[@class="source"]//text()')[-1] if row.xpath('div[@class="cont"]/p[@class="source"]//text()') else ''
            content = ''.join(row.xpath('div[@class="cont"]/div[@class="contson"]//text()')).replace('  ', '').replace('\n', '') if row.xpath('div[@class="cont"]/div[@class="contson"]//text()') else ''
            tag = ','.join(row.xpath('div[@class="tag"]/a/text()')) if row.xpath('div[@class="tag"]/a/text()') else ''
            self.db.add_new_row('shigeSpider', { 'title': title, 'dynasty': dynasty, 'author': author, 'content': content, 'tag': tag, 'createTime': str(date.today()) })
            print 'Title: {}'.format(title)
        if response.xpath('//div[@class="pages"]/a/@href'):
            self.download('http://' + self.domain + response.xpath('//div[@class="pages"]/a/@href')[-1])

if __name__ == '__main__':
    sys.setrecursionlimit(100000)
    url = 'http://so.gushiwen.org/type.aspx'
    do = shigeSpider()
    do.download(url)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档