python2+selenium爬取笔趣读小说

python2+selenium爬取笔趣读小说python2+selenium爬取笔趣读小说 #!/usr/bin/envpython#coding=utf-8fromseleniumimportwebdriverimporttimefrombs4importBeaut…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

#! /usr/bin/env python
#coding=utf-8

from selenium import webdriver
import time
from bs4 import BeautifulSoup

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

browser = webdriver.Firefox()

#获取文章标题和内容,并写入文档
def get_article():
    title = browser.find_element_by_xpath('//div[@class="bookname"]/h1').text
    print title

    content = browser.find_element_by_id('content').text
    #print content

    with open ('storytudou.txt', 'a') as f:
        f.write(title + '\n')
        f.write(content + '\n\n')

#获取该本小说共有多少章
def page_num():
    browser.get("https://www.biqudu.net/31_31729/")
    html = browser.page_source
    soup = BeautifulSoup(html, 'lxml')
    dd = soup.find_all('dd')
    #print dd    
    page = len(dd)
    return page

#点击下一章
def index_page(i):
    if i == 1:
        browser.get("https://www.biqudu.net/31_31729/2212637.html")
        time.sleep(10)
    get_article()
    js = "window.scrollTo(0,document.body.scrollHeight)"
    browser.execute_script(js)
    time.sleep(5)    
    next_p = browser.find_element_by_xpath('//div[@class="bottem2"]/a[3]')
    #next_p = browser.find_element_by_xpath('/html/body/div/div[5]/div[2]/div[5]/a[3]')
    time.sleep(5)
    next_p.click()
    time.sleep(10)

 #遍历小说全部章节       
def main():
    page = page_num()
    print(page)
    for i in range(1, page+1):
        index_page(i)
        
if __name__ == '__main__':
    
    main()
        

Jetbrains全家桶1年46,售后保障稳定

系统:ubuntu

需要的安装BeautifulSoup

yanner@yanner-VirtualBox:~$ sudo apt-get install python-bs4

说明:

Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象;

soup.find_all(‘dd’)  获取所有的P标签,返回一个列表,类型为’bs4.element.Tag’。

posted on
2019-08-20 15:01 
yanner 阅读(
) 评论(
)
编辑 收藏

转载于:https://www.cnblogs.com/yanner/p/11382946.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/200894.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号