python爬虫demo01
python爬虫demo01

1 import requests, json, time, sys
2 from bs4 import BeautifulSoup
3 from contextlib import closing
4
5 url = 'https://image.xiaozhustatic1.com/12/9,0,27,3473,1800,1200,d064ccfb.jpg'
6 headers = {
7 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
8 }
9
10 url2 = 'http://bj.xiaozhu.com/'
11
12
13 def get_img_src_list(url):
14 """
15 获取图片的url
16 :param url:
17 :return:
18 """
19 res = requests.get(url, headers=headers)
20 res_data = BeautifulSoup(res.text, 'lxml')
21 imgs = res_data.select('#page_list > ul > li > a > img')
22 # titles = res_data.select('#page_list > ul > li > div.result_btm_con.lodgeunitname > div > a > span')
23 # print( imgs )
24 img_srcs = []
25 img_alt = []
26 for img in imgs:
27 img_srcs.append(img.get('lazy_src'))
28 return img_srcs
29
30
31 def downloadPic(url, pic_name):
32 """
33 下载图片
34 :param url:
35 :param pic_name:
36 :return:
37 """
38 res = requests.get(url, headers=headers, stream=True)
39 with closing(res) as r:
40 with open('%d.jpg' %pic_name, 'ab+') as f:
41 for chunk in res.iter_content(chunk_size=1024):
42 if chunk:
43 f.write(chunk)
44 f.flush()
45 print('下载{}.jpg成功!'.format(pic_name))
46
47 if __name__ == '__main__':
48 img_srcs = get_img_src_list(url2)
49 for i in range(len(img_srcs)):
50 print(img_srcs[i])
51 # time.sleep(1)
52 downloadPic(img_srcs[i], i)
53 i += 1

python爬虫demo01的更多相关文章
- 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 ...
- 批量下载小说网站上的小说(python爬虫)
随便说点什么 因为在学python,所有自然而然的就掉进了爬虫这个坑里,好吧,主要是因为我觉得爬虫比较酷,才入坑的. 想想看,你可以批量自动的采集互联网上海量的资料数据,是多么令人激动啊! 所以我就被 ...
- python 爬虫(二)
python 爬虫 Advanced HTML Parsing 1. 通过属性查找标签:基本上在每一个网站上都有stylesheets,针对于不同的标签会有不同的css类于之向对应在我们看到的标签可能 ...
- Python 爬虫1——爬虫简述
Python除了可以用来开发Python Web之后,其实还可以用来编写一些爬虫小工具,可能还有人不知道什么是爬虫的. 一.爬虫的定义: 爬虫——网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区 ...
- Python爬虫入门一之综述
大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...
- [python]爬虫学习(一)
要学习Python爬虫,我们要学习的共有以下几点(python2): Python基础知识 Python中urllib和urllib2库的用法 Python正则表达式 Python爬虫框架Scrapy ...
随机推荐
- 【爬虫集合】抖音API分析
1. 分析接口 Charles注册码 Registered Name: https://zhile.io License Key: 48891cf209c6d32bf4 抖音API分析 抖音.猫眼网页 ...
- sping+redis实现消息队列的乱码问题
使用spring支持redis实现消息队列,参考官方样例:https://spring.io/guides/gs/messaging-redis/ 实现后在运行过程中发现消费者在接收消息时会出现乱码的 ...
- Html5+Mui前端框架,开发记录(四):下拉菜单绑定数据、搜索、时间控件
1.下拉菜单绑定数据,选择后回传值 1)html: <div class="mui-input-row"> <label>xxx:</label> ...
- [C#] LINQ之SelectMany和GroupJoin
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Unity使用Resources读取Resources路径下的二进制文件(Binary Data)必须使用 .bytes扩展名
将某二进制文件放在Resources目录下,希望用Resources.Load<TextAsset>的方式读取,发现TextAsset是null 查阅Unity文档得知,使用Resourc ...
- S2-016、S2-017
前言 由于S2-016.S2-017出现的原因时相同的,只是由于poc不一样,造成了不同的攻击.S2-016是RCE,S2-017是开发型重定向漏洞.这里将两个漏洞放一起分析.另外“Struts2系列 ...
- Array + two points leetcode.18 - 4Sum
题面 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in num ...
- Nginx从安装到简单使用
一.什么是Nginx: Nginx是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务. 二.Nginx作用: 反向代理,集群,虚拟服务器,负载均衡,动静分离,解决跨域问题等 ...
- springboot学习链接
https://github.com/wuyouzhuguli/SpringAll
- Robot Framework--运行pybot时出错
1.在Execution Profile中选择 pybot,点击 start,报错,找不到指定文件 2.在cmd中运行pybot.bat也是报错---pybot is not define 3.找到p ...