python爬虫学习
提前声明:请勿他用,仅限个人学习
运用模块有

import requests
import re
import os

较为常规,适合网络小白。lxml和bs4也是基础。长话短说。

headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 Edg/81.0.416.72'}
link="https://www.woyaogexing.com/touxiang/qinglv/"#编写请求头信息
r=requests.get(link,headers=headers)
r.encoding=r.apparent_encoding
html=r.text
# print(html)


编写请求头,和要获取的网址,link,一般常用url,只是一个简称。个人习惯吧。
然后开始分析这个网站,这次用到的是re

运用正则表达式找到那段文字,

title=re.findall('<div class="h1-title z"><h1>(.*?)</h1><i></i><span>></span></div>',html)
divs=re.compile('<a href="(.*?)" class="img" target="_blank" title=".*?">')
divs=re.findall(divs,html)
# print(divs)

测试一下,开始使用迭代语句,进入我们真正想要爬取的图片地址

for div in divs:links='https://www.woyaogexing.com'+divresp=requests.get(links,headers=headers)resp.encoding=resp.apparent_encodinghtmls=resp.text# print(htmls)

到我们找到之后,links就是我们要找的网址,完善这个网址,然后开始第二次请求
首先用到正则表达式,获取我们的第二次想要爬取的网址

hrefs=re.compile('<a href="(.*?)" class="swipebox">')hrefs=re.findall(hrefs,htmls)ids=re.findall('<h1>(.*?)</h1>',htmls)

同时编辑好,存储的路径,用到os模块,字符里面有‘

 base_path = 'F://我要个性网/%s'%titlefor id in ids:id=re.sub('[/]+','--',id)#字符里面有/影响我们存储,去掉path = os.path.join(base_path, id)  # 创建路径if not os.path.exists(path):os.makedirs(path)

最后一步,获取href,以content形式下载保存

    for href in hrefs:href='https:'+href# print(href)tupian=requests.get(href,headers=headers)with open(str(path)+'/'+href.split('/')[-1]+'.jpeg','wb')as f:f.write(tupian.content)print('正在下载中{}'.format(href.split('/')[-1]))

完美收工。
全代码

import requests
import re
import os
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 Edg/81.0.416.72'}
link="https://www.woyaogexing.com/touxiang/qinglv/"#编写请求头信息
r=requests.get(link,headers=headers)
r.encoding=r.apparent_encoding
html=r.text
# print(html)
title=re.findall('<div class="h1-title z"><h1>(.*?)</h1><i></i><span>></span></div>',html)
divs=re.compile('<a href="(.*?)" class="img" target="_blank" title=".*?">')
divs=re.findall(divs,html)
# print(divs)
for div in divs:links='https://www.woyaogexing.com'+divresp=requests.get(links,headers=headers)resp.encoding=resp.apparent_encodinghtmls=resp.text# print(htmls)hrefs=re.compile('<a href="(.*?)" class="swipebox">')hrefs=re.findall(hrefs,htmls)ids=re.findall('<h1>(.*?)</h1>',htmls)base_path = 'F://我要个性网/%s'%titlefor id in ids:id=re.sub('[/]+','--',id)path = os.path.join(base_path, id)  # 创建路径if not os.path.exists(path):os.makedirs(path)for href in hrefs:href='https:'+href# print(href)tupian=requests.get(href,headers=headers)with open(str(path)+'/'+href.split('/')[-1]+'.jpeg','wb')as f:f.write(tupian.content)print('正在下载中{}'.format(href.split('/')[-1]))


nice!
安排一波

python爬虫我要个性网,获取头像相关推荐

  1. python爬虫爬猎聘网获取多条职责描述中有Linux需求的招聘信息

    python爬虫爬猎聘网获取多条职责描述中有Linux需求的招聘信息 下列是我爬虫的作业 摘 要 随着现代化社会的飞速发展,网络上巨大信息量的获取给用户带来了许多的麻烦.由于工作和生活节奏的需求,人们 ...

  2. 利用python爬取qq个性网图片

    利用python爬取qq个性网图片 网站头像布局大同小异,稍改代码即可爬取想要的头像. 不多bb,上代码. import requests from parsel import Selector im ...

  3. python爬虫爬取音乐_利用python爬虫实现爬取网易云音乐热歌榜

    利用python爬虫实现爬取网易云音乐热歌榜 发布时间:2020-11-09 16:12:28 来源:亿速云 阅读:102 作者:Leah 本篇文章给大家分享的是有关利用python爬虫实现爬取网易云 ...

  4. Python爬虫登录大学官网

    Python爬虫登录大学官网   通过python登录大学官网(当然首先要有账号密码),内容包括:如何使用chrome查看网页信息和网络请求.分析网站通过js加密用户密码的方式.使用python登录网 ...

  5. python爬虫爬取当当网的商品信息

    python爬虫爬取当当网的商品信息 一.环境搭建 二.简介 三.当当网网页分析 1.分析网页的url规律 2.解析网页html页面 书籍商品html页面解析 其他商品html页面解析 四.代码实现 ...

  6. 用Python爬虫来爬写真网图片

    用Python爬虫来爬写真网图片 1.我们先要知道Python爬虫的原理 基本的Python爬虫原理很简单,分为三步 获取网页源码 通过分析源码并通过代码来获取其中想要的内容 进行下载或其他操作 话不 ...

  7. Python爬虫_某宝网案例

    Python爬虫_某宝网案例 一.导入第三方库,确定url,定义headers ,伪装爬虫代码 import requests url = 'https://s.taobao.com/search?q ...

  8. [Python爬虫案例]-中国古诗网

    [Python爬虫案例]-中国古诗网 看懂代码,你需要相关知识 爬虫必备知识 只是想得到目标的话,直接运行就好了 import requests import re import jsondef pa ...

  9. python爬虫爬取知网

    python爬虫爬取知网 话不多说,直接上代码! import requests import re import time import xlrd from xlrd import open_wor ...

最新文章

  1. 泰晤士高等教育2020年新兴经济体大学排名出炉,81所中国大陆高校上榜!
  2. 心得丨学习人工智能AI需要哪些最基础的知识?
  3. 看完50多家阵亡的初创企业,我们发现了AI创业的5个行业潜规则
  4. mfc倾斜文本输入_文本检测知识梳理(持续更新)
  5. WindowsXP中修改本地路由表
  6. win7设置开机后自动锁定计算机,Win7系统锁定计算机怎么设置 win7系统自动锁定...
  7. Python 基础教程:切片、迭代和列表生成式
  8. 【瞎扯】我的OI之路
  9. northwind中文 for mysql_学习心得 | PHP与mysql通信的若干问题
  10. 01-UIScrollView01-大图片展示
  11. 10年IT老兵酒后吐真言,我看了5遍...
  12. _软件园三期西片区F地块举行招商推介会 超300家企业意向落户 - 本网原创
  13. linux yum 安装播放器,centos6.5 常用影音播放器安装
  14. 这四款录屏工具,也许是电脑录屏软件中免费、无广告且最实用的
  15. VC2015 运行库安装错误 0x80240017 解决过程
  16. nginx autoindex美化
  17. 设置边框大小html,css border-width属性设置边框宽度
  18. 2021-07-17 随笔
  19. 三角形内切圆和外接圆半径及其面积计算
  20. ChatGPT封杀潮,禁入学校,AI顶会特意改规则,LeCun:要不咱把小模型也禁了?...

热门文章

  1. opencv3/C++图像边缘提取canny算子与Sobel算子实现opencv(VS2019 C++)
  2. 计算机组成原理之定点数与浮点数
  3. ZSV08-31、DHF08-231、SV08-B20M、LSV-08-2NCP-M插装式电磁阀Z
  4. NR 通信中的相干时间Tc和相干带宽Wc
  5. FreeType与CFF
  6. Virtualbox6.1 共享文件夹设置
  7. 重装助手教你如何打开或者关闭粘滞键
  8. 纸黄金投资中的技术分析技巧
  9. 教师使用计算机责任书,计算机教师消防安全责任书.doc
  10. SysTick 定时器