关于python:Python-爬取小说斗破之无上

9次阅读

共计 1257 个字符,预计需要花费 4 分钟才能阅读完成。

极客都是这么看小说
  • 代码为自己原创,且仅限于学习交换,请勿用于任何商业用途!自己不承当任何法律责任,如果波及到侵权问题,请留言告知。
import requests
from bs4 import BeautifulSoup


class downloader(object):

    def __init__(self):
        self.base = 'http://m.yruan.com'
        self.article = '/article/53567/'
        self.page = '31977800_3.html'
        self.target = self.base + self.article + self.page

    def get_contents(self):
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                          'Chrome/70.0.3538.67 Safari/537.36',
        }
        params = {
            'kw': '赵丽颖吧',
            'pn': '50'
        }
        req = requests.get(url=self.target, headers=headers, params=params)
        # requests.post()
        req.encoding = 'utf-8'
        html = req.text
        bs = BeautifulSoup(html, 'html.parser')  # 失去一个 BeautifulSoup 的对象
        # print(bs.prettify())
        # print(bs.getText())
        novelcontent = bs.find(name='div', id="novelcontent")
        print(type(novelcontent))
        # print(novelcontent.find(name='p').text.replace('\n', ''))
        # print(novelcontent.find(name='p'))
        print(novelcontent.find(name='p').text.replace('\n\n', '\n'))
        # print(novelcontent.find(name='ul', class_='novelbutton'))
        next_a = novelcontent.find(name='ul', class_='novelbutton').find(name='p', class_='p1 p3').find(name='a')
        print(next_a.get('href'))
        # print(novelcontent)
        print("current_url:" + self.target)
        return self.base + next_a.get('href')


if __name__ == '__main__':
    dl = downloader()
    next_url = dl.get_contents()
    print("Next_url:" + next_url)
正文完
 0