import urllib
from time import sleep import requests
from lxml import etree try:
def all_links(url,page):
# if "900.html" in url:
# print("结束");
# return None
url = url + str(page) + ".html";
response = requests.get(url)
print(url, response.status_code)
html = etree.HTML(response.content.decode('gbk'))
## 获取图片 并且保存
imgs = html.xpath('.//div[@id="wrapper"]//div[@class="ui-module"]//img/@src')
for img in imgs:
file_name = img.split('/')[-1]
first = img.split('/')[0]
if first != 'http:' and first != 'https:':
print("错误图片"+img)
else:
dir_path = "/www/spider/images/"
try:
file_content = requests.get(img)
if file_content.status_code != 200:
print(img,"下载失败")
else: #urllib.request.urlretrieve(img, dir_path + file_name)
with open(dir_path+file_name,"wb") as f:
f.write(file_content.content)
print("保存图片" + dir_path + file_name + "成功")
except Exception as ee:
print(str(ee))
# links = html.xpath('.//div[@class="page"]//a[contains(text(),"下一页")]/@href')
# print(links)
# if len(links) < 1:
# pass
# else:
sleep(1)
host = 'http://www.qiubaichengren.net/'
next_page = page + 1
all_links(host,next_page) for i in range(1,991):
all_links("http://www.qiubaichengren.net/",354)
except Exception as e:
print(str(e))

循环的版本

import urllib
from time import sleep import requests
from lxml import etree try:
def all_links(url):
if "100.html" in url:
print("结束");
return None
response = requests.get(url)
print(url, response.status_code)
html = etree.HTML(response.content.decode('gbk'))
## 获取图片 并且保存
imgs = html.xpath('.//div[@id="wrapper"]//div[@class="ui-module"]//img/@src')
for img in imgs:
file_name = img.split('/')[-1]
first = img.split('/')[0]
if first != 'http:' and first != 'https:':
print("错误图片"+img)
else:
dir_path = "d:\\www\\spider\\images\\"
urllib.request.urlretrieve(img, dir_path + file_name)
print("保存图片" + dir_path + file_name + "成功")
links = html.xpath('.//div[@class="page"]//a[contains(text(),"下一页")]/@href')
print(links)
if len(links) < 1:
pass
else:
sleep(5)
host = 'http://www.qiubaichengren.net/'
new_url = host + links[0];
all_links(new_url)
all_links("http://www.qiubaichengren.net/8.html")
except Exception as e:
print(str(e))

python 爬虫 糗百成人的更多相关文章

  1. Python爬虫实例:糗百

    看了下python爬虫用法,正则匹配过滤对应字段,这里进行最强外功:copy大法实践 一开始是直接从参考链接复制粘贴的,发现由于糗百改版导致失败,这里对新版html分析后进行了简单改进,把整理过程记录 ...

  2. Python爬虫学习:四、headers和data的获取

    之前在学习爬虫时,偶尔会遇到一些问题是有些网站需要登录后才能爬取内容,有的网站会识别是否是由浏览器发出的请求. 一.headers的获取 就以博客园的首页为例:http://www.cnblogs.c ...

  3. Python爬虫——Python 岗位分析报告

    前两篇我们分别爬取了糗事百科和妹子图网站,学习了 Requests, Beautiful Soup 的基本使用.不过前两篇都是从静态 HTML 页面中来筛选出我们需要的信息.这一篇我们来学习下如何来获 ...

  4. python 爬虫(转,我使用的python3)

      原文地址:http://blog.csdn.net/pi9nc/article/details/9734437 [Python]网络爬虫(一):抓取网页的含义和URL基本构成 分类: 爬虫 Pyt ...

  5. Python爬虫(一)爬百度贴吧

    简单的GET请求: # python2 import urllib2 response = urllib2.urlopen('http://www.baidu.com') html = respons ...

  6. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  7. python爬虫成长之路(一):抓取证券之星的股票数据

    获取数据是数据分析中必不可少的一部分,而网络爬虫是是获取数据的一个重要渠道之一.鉴于此,我拾起了Python这把利器,开启了网络爬虫之路. 本篇使用的版本为python3.5,意在抓取证券之星上当天所 ...

  8. python爬虫学习(7) —— 爬取你的AC代码

    上一篇文章中,我们介绍了python爬虫利器--requests,并且拿HDU做了小测试. 这篇文章,我们来爬取一下自己AC的代码. 1 确定ac代码对应的页面 如下图所示,我们一般情况可以通过该顺序 ...

  9. python爬虫学习(6) —— 神器 Requests

    Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 H ...

随机推荐

  1. CRM2Stark组件

    CRM stark组件 开启新项目:CRM 再创建一个应用app02(python manage.py startapp app02 或者是工具栏:run...task:执行startapp app0 ...

  2. (杭电 1702)ACboy needs your help again!

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  3. STL---llist

    #include<iostream> #include<algorithm> #include<list> using namespace std; struct ...

  4. app与php后台接口登录认证、验证(seesion和token)

    简要:随着电商的不断发展,APP也层次不穷,随着科技的发展主要登录形式(微信.QQ.账号/密码):为此向大家分享一下"app与php后台接口登录认证.验证"想法和做法:希望能够帮助 ...

  5. IP数据报、TCP报文、UDP报文格式

    总是记不得TCP/IP协议的各个协议格式,特在此做个记录,好方便回顾. 信息来自众多网络大神们的总结,我再结合自己的理解整理所得. ================================== ...

  6. python--复习之路的目录

    想要看时点链接看看,常来复习,温故而知新,可以为师矣. 1:基本类型 python--基本类型之字符串 python--基本类型之列表 python--基本类型之元组 python--基本类型之集合 ...

  7. Java编辑环境搭建

    1.Java开发环境搭建 这里主要说的是在Windows系统下的环境搭建 JDK的安装 java的sdk简称JDK ,去其官方网站下载最近的JDK即可http://www.oracle.com/tec ...

  8. mongdb数据迁移导出与导入

    导出: mongoexport --host localhost --port --username un1 --password pwd1 --db db1 --collection col1 -- ...

  9. beauifulsoup模块的介绍

    01   爬虫基础知识介绍 相关库:1.requests,re  2.BeautifulSoup   3.hackhttp 使用requests发起get,post请求,获取状态码,内容: 使用re匹 ...

  10. Qt listwigwt item 加入自定义元素

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...