python 爬虫 糗百成人
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 爬虫 糗百成人的更多相关文章
- Python爬虫实例:糗百
看了下python爬虫用法,正则匹配过滤对应字段,这里进行最强外功:copy大法实践 一开始是直接从参考链接复制粘贴的,发现由于糗百改版导致失败,这里对新版html分析后进行了简单改进,把整理过程记录 ...
- Python爬虫学习:四、headers和data的获取
之前在学习爬虫时,偶尔会遇到一些问题是有些网站需要登录后才能爬取内容,有的网站会识别是否是由浏览器发出的请求. 一.headers的获取 就以博客园的首页为例:http://www.cnblogs.c ...
- Python爬虫——Python 岗位分析报告
前两篇我们分别爬取了糗事百科和妹子图网站,学习了 Requests, Beautiful Soup 的基本使用.不过前两篇都是从静态 HTML 页面中来筛选出我们需要的信息.这一篇我们来学习下如何来获 ...
- python 爬虫(转,我使用的python3)
原文地址:http://blog.csdn.net/pi9nc/article/details/9734437 [Python]网络爬虫(一):抓取网页的含义和URL基本构成 分类: 爬虫 Pyt ...
- Python爬虫(一)爬百度贴吧
简单的GET请求: # python2 import urllib2 response = urllib2.urlopen('http://www.baidu.com') html = respons ...
- Python 爬虫模拟登陆知乎
在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...
- python爬虫成长之路(一):抓取证券之星的股票数据
获取数据是数据分析中必不可少的一部分,而网络爬虫是是获取数据的一个重要渠道之一.鉴于此,我拾起了Python这把利器,开启了网络爬虫之路. 本篇使用的版本为python3.5,意在抓取证券之星上当天所 ...
- python爬虫学习(7) —— 爬取你的AC代码
上一篇文章中,我们介绍了python爬虫利器--requests,并且拿HDU做了小测试. 这篇文章,我们来爬取一下自己AC的代码. 1 确定ac代码对应的页面 如下图所示,我们一般情况可以通过该顺序 ...
- python爬虫学习(6) —— 神器 Requests
Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 H ...
随机推荐
- python3.5 安装twisted
https://blog.csdn.net/caimouse/article/details/77647952 下载地址:http://www.lfd.uci.edu/~gohlke/pythonli ...
- win10 禁用自动更新
管理员身份运行CMD,输入REG add "HKLM\SYSTEM\CurrentControlSet\Services\UsoSvc" /v "Start" ...
- java中集合部分的笔记
集合类框架概要Collection<E> boolean removeIf(Predicate<? super E> filter)//删除满足条件的元素,如果Collecti ...
- 【8086汇编-Day8】实验九
Lab1 代码 ; 在屏幕上输出内存单元中的十进制两位数 assume cs:code, ds:data data segment db db , ; 前一个字节用于保存商,后一个字节用于保存余数 d ...
- Touch table
On this page I present the results of my touch action research. I concentrated on the few basic acti ...
- substr是不安全的
今天遇到一个问题,数据库中保存的内容通过php在页面无法显示,如果将内容换行或加个空格或者随便加点其他内容就能正常显示. 非常的诡异,显示的内容是通过截取得到的.代码非常简单 substr($pMar ...
- Python 列表下标操作
Python 列表下标操作 引用网址: https://www.jianshu.com/p/a98e935e4d46
- 阿里云ECS下CentOS7.4 yum安装Python3.6环境
一.安装EPEL和IUS软件源 二.安装Python3.6 三.创建python3软链接连接符 四.安装pip3 五.创建pip3链接符 六.进行验证是否安装成功 一.安装EPEL和IUS软件源 yu ...
- TPO-14 C2 Prepare for a career in journalism
TPO-14 C2 Prepare for a career in journalism 第 1 段 1.Listen to a conversation between a student and ...
- Siki_Unity_1-9_Unity2D游戏开发_Roguelike拾荒者
Unity 1-9 Unity2D游戏开发 Roguelike拾荒者 任务1:游戏介绍 Food:相当于血量:每走一步下降1,吃东西可以回复(果子10药水20),被怪物攻击会减少中间的障碍物可以打破, ...