import requests
from lxml import etree
from bs4 import BeautifulSoup
import os
from selenium import webdriver #解析每个漫画分页并下载漫画
def manhua(url): browser.get(url) #获取模拟访问的页面源码
html=browser.page_source html = etree.HTML(html)
img_url = html.xpath('//img[@id="mangaFile"]/@src')[0]
alt = html.xpath('/html/body/div[2]/div[2]/h1/a/text()')[0]
title = html.xpath('/html/body/div[2]/div[2]/h2/text()')[0]
print(img_url,alt,title) # print(html) path='./漫画/'+alt+'/'+title+'/'
if not os.path.exists(path):
os.makedirs(path)
fname=img_url.split('/')[-1]
# print(fname) print(os.path.join(path,fname)) # request.urlretrieve(img_url,os.path.join(path,fname)) #请求图片地址
response = requests.get(img_url)
#二进制解码
data= response.content
#保存文件
with open(path+fname,'wb') as f:
f.write(data)
#解析获取漫画分页链接
def manhua_url(url):
response = requests.get(url)
response.encoding = response.apparent_encoding
html = response.text
html = etree.HTML(html)
# print(html)
#i为漫画页数
i = html.xpath('/html/body/div[2]/div[2]/span/text()')[1][1:-1]
i=int(i)
# print(i)
#找到分页规律
#拼接分页链接,选择用format函数
url = url +'/index.html?p={}'
# print(url)
for n in range(1,i+1):
fullurl = url.format(n)
print(fullurl)
# time.sleep(2)
#fullurl为所有的分页漫画链接
manhua(fullurl) #解析列表页
def list(lb_url):
response = requests.get(lb_url)
response.encoding = response.apparent_encoding
html = response.text
html = BeautifulSoup(html,'lxml')
#匹配所有章节链接
url_list = html.select('div.subBookList ul li')
for url in url_list :
url = url.select('a')[0].get('href').split('/')[-2] # print(url)
fullurl = os.path.join(lb_url,url)
print(fullurl)
#章节链接
manhua_url(fullurl) # print(url_list)
# print(html) #解析首页
def shouye():
#首页链接
base_url = 'http://www.omanhua.com/'
#发起请求
response = requests.get(base_url)
#解码
response.encoding = response.apparent_encoding
#获取返回的网页
html = response.text
# print(html)
#解析
html =BeautifulSoup(html,'lxml')
#匹配最热漫画链接
url_list = html.select('ul#cartoon_image_show1 li')
for url in url_list:
# print(url)
url = url.select('a')[0].get('href')[1:]
# alt = url.select('a')
# print(alt)
#拼接链接
fullurl = os.path.join(base_url,url)
print(fullurl) list(fullurl)
if __name__ == '__main__':
# 用自动测试模块selenium模拟浏览器访问,这里用谷歌 图片加载获取不到图片链接
#后面的路径是chorm驱动路径
browser = webdriver.Chrome(executable_path=r'C:\Users\zhaozhi\Desktop\chromedriver.exe')
shouye() 刚开始自学爬虫不久,代码可能写的有点繁琐,希望和大家一起学习学习进步

最新文章

  1. jquery网页换肤+jquery的cookie+动态调用css样式文件,可以的
  2. 获取$(this)子节点对象的方法
  3. AutoIt3(AU3)开发的智能驱动安装工具,用于系统封装,支持参数静默启动
  4. WPF学习开发客户端软件-任务助手(已上传源码)
  5. Eclipse-将svn上的项目转化成相应的项目
  6. [BZOJ4636]蒟蒻的数列
  7. XHProf的安装和使用(PHP性能测试神器)
  8. C#的cs文件中Response.Clear();Response.ClearHeaders()的作用
  9. 利用分布类防止EF更新模型丢失验证信息
  10. jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部
  11. python并发编程之多进程二
  12. [译]ASP.NET Core依赖注入深入讨论
  13. Abp通用配置模块的设计
  14. redis消息队列,tp5.0,高并发,抢购
  15. [Swift]LeetCode58. 最后一个单词的长度 | Length of Last Word
  16. Forward团队-爬虫豆瓣top250项目-项目总结
  17. awk基本用法
  18. Java多线程编程模式实战指南(三):Two-phase Termination模式
  19. 第87讲:scala中使用For表达式做查询
  20. [leetcode]Minimum Depth of Binary Tree--二叉树层序遍历的应用

热门文章

  1. Delphi - 本地路径的创建、清空本地指定文件夹下的文件
  2. Oracle误操作--被提交后的数据回退(闪回)
  3. CodeForces - 940E - Cashback +贪心+DP
  4. hdu 1671 Phone List 字典树模板
  5. LeetCode380 常数时间插入、删除和获取随机元素
  6. 网络流---最大流(Edmond-Karp算法)的学习
  7. NameNode数据存储
  8. virtualbox下给centos7固定ip
  9. 配置Linux使用LDAP用户认证
  10. ImageView的功能和使用