美文网首页
爬取诗词名句网

爬取诗词名句网

作者: 寻找u | 来源:发表于2018-10-16 15:20 被阅读0次

import urllib.request

from bs4 import BeautifulSoup

import time

import os

def handle_request(url):

headers={

'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36'

}

request = urllib.request.Request(url=url,headers=headers)

return request

def parse_content(content):

soup = BeautifulSoup(content,'lxml')

every_book_url = soup.select('.bookmark-list > ul > li > h2 > a')

return every_book_url

def parse_content2(content2):

soup = BeautifulSoup(content2,'lxml')

book_title = soup.select('.book-header > h1')[0].string.strip('《》')

filenam = os.mkdir('%s'%book_title)

filename = '{}\\'.format(filenam)

# print(typ?e(filename))

book_name = book_title + '.txt'

print(book_name)

content_urls = soup.select('.book-mulu > ul > li > a')

filename = r"{}\\".format(book_title)

filepath = os.path.join(filename, '{}.txt'.format(book_title))

print(filepath)

fp = open(filepath,'w',encoding='utf8')

for content_url in content_urls:

href = 'http://www.shicimingju.com' + content_url['href']

try:

title = content_url.string

print('正在下载--%s--' % title)

text = get_text(href)

content4 = title + '\n' + text

# urllib.request.urlretrieve(book_name,filepath)

# filename = r"C:\\Users\\Administrator\\Desktop\\pachong\\book"

# filepath = os.path.join(filename, '{}.txt'.format(book_title))

# print(filepath)

# fp = open(filepath,'w',encoding='utf8')

fp.write(content4)

except:

print('这个不能下载')

print('结束下载--%s--' % title)

fp.close()

def get_text(href):

request = handle_request(href)

content3 = urllib.request.urlopen(request).read().decode('utf8')

soup = BeautifulSoup(content3,'lxml')

# booktitle = soup.select('.nav-top > a')[2].string

# book_name = os.mkdir('%s'%booktitle)

# book_name = booktitle + '.txt'

# print(book_name)

# exit()

cha_content = soup.find('div',class_="chapter_content").text

return cha_content

# text = cha_content.text

# filename = r"C:\\Users\\Administrator\\Desktop\\pachong\\book"

# filepath = os.path.join(filename, book_name)

# print(filepath)

# fp = open(filepath,'w',encoding='utf8')

# fp.write(title + '\n' + text)

# print('结束下载--%s--' % title)

# fp.close()

def main():

# fp = open(filepath, 'w', encoding='utf8')

url = 'http://www.shicimingju.com/book/'

request = handle_request(url)

content = urllib.request.urlopen(request)

request = parse_content(content)

for book_url in request:

href = 'http://www.shicimingju.com' + book_url['href']

request2 = handle_request(href)

content2 = urllib.request.urlopen(request2)

parse_content2(content2)

time.sleep(3)

if __name__ == '__main__':

main()

相关文章

  • 爬取诗词名句网

    import urllib.request from bs4 import BeautifulSoup impor...

  • Selenium小例子

    爬取腾讯动漫 爬取某网站漫画 爬取拉勾网

  • 爬取中华诗词网所有诗词

    今天给大家分享一个爬取中华诗词网所有诗词的python爬虫,本项目主要使用了pyquery库和request库(其...

  • 爬煎蛋网妹子图

    利用 BeautifulSoup + Requests 爬取 煎蛋网 妹子图 一、爬煎蛋网一页图片 此爬虫只能爬取...

  • Python:爬虫(2019-1-15)

    爬取当当网书籍数据 爬取当当数据,并排序输出

  • Python学习笔记7——爬取大规模数据

    我们在爬取数据时,往往是连续爬取上百个页面,本篇以爬取赶集网为例,爬取大规模的数据。步骤如下: 爬取1级商品链接 ...

  • Java——jsoup爬取中国诗词网

    1.准备工作 中国诗词网网址:http://www.shicimingju.com/开发平台:Ubentu18.1...

  • Python·爬取当当网图书信息

    爬虫实践 爬取当当网图书信息 爬取豆瓣即将上映电影爬虫 1)爬取常用的数据结构模型 2)图片爬取 3) 批量命名图片

  • 爬取拉勾网

    拉勾网数据加载的方式使用的是ajax异步加载的方式从后端加载数据,所以就需要分析加载的URL,如果有疑问可以看我的...

  • day71-代理ip的使用

    1代理ip 在爬取网页过程中,经常遇到爬取次数过于频繁而被封ip地址,这个时候想要再次爬取就要使用代理ip来爬取网...

网友评论

      本文标题:爬取诗词名句网

      本文链接:https://www.haomeiwen.com/subject/qqehzftx.html