scrapy爬虫-爬取wattpad外网小说网站

目前还在学习爬虫scrapy框架,尝试爬取外网的小说网站:https://www.wattpad.com/stories/adventure
目前只是实现了一部分非常简单的内容爬取
还未实现的功能
‘’‘

1、将parse1和parse2 的内容结合到一起

2、没有实现根据阅读量去提取作者信息

3、没有实现将内容保存到数据库中

‘’‘’
有大佬看了可以指点一下吗?
代码如下:

import scrapy
import re
import urllib.response as ur
import lxml.etree as le



# 1、将parse1和parse2 的内容结合到一起
# 2、没有实现根据阅读量去提取作者信息
# 3、没有实现将内容保存到数据库中




class WpSpider(scrapy.Spider):
    name = 'wp'
    # allowed_domains = ['wattpad']

    def start_requests(self):
        # yield scrapy.Request(
        #     url='https://www.wattpad.com/stories/adventure',
        #     callback=self.parse1
        # )

        yield scrapy.Request(
            url='https://www.wattpad.com/stories/adventure',
            callback=self.parse2
        )



    #实现提取一级页面的基本信息(书名,作者,阅读量)
    def parse1(self,response):
        content_x_s = response.xpath('//div[@class="content"]')
        for content_x in content_x_s:
            title_S = content_x.xpath('./a[1]/text()').extract_first()
            Author_S= content_x.xpath('./a[2]/text()').extract_first()
            readCount_S = content_x.xpath('./div/span[1]/text()').extract_first()
            # description_S = content_x.xpath('./div[2]/text()').extract_first()

            # print('parse1:', title_S, Author_S, readCount_S)



    #根据有阅读量取到作者信息也提取作者公共社交账户链接
    def parse2(self,response):
        read_x_s = response.xpath('//div[@class="meta social-meta"]/span[1]/../../a[2]/@href').extract()
        '''
        还未实现的功能"根据阅读量去提取作者信息"
        #  for readCount in read_x_s:
        #     read = float(readCount[:-1])
        #     if read >= 100:
        #         li_s = le.HTML(readCount).xpath('./../../../a[2]/@href')
        
        '''


        for h_x in read_x_s:
            url_author = 'https://www.wattpad.com/' + h_x
            yield scrapy.Request(
                url=url_author,
                callback=self.info
            )
        # href_x_s = le.HTML(html).xpath('//div[@class="meta social-meta"]/../a[2]/@href')
        # print()

        def info(self,response):
        web_x_s = response.xpath('//div[@class="description"]/pre/a/@href').extract()
        for web_x in web_x_s:
            if web_x == '/user/@@@':
                pass
            else:
                print('parse2:',web_x)


parse1输出如下:
在这里插入图片描述
parse2输出如下:
因为有的作者主页没有添加个人社交媒体账号所以没有返回数据
但有社交媒体链接的依然可以提取到的url
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42873348/article/details/108713485