python爬虫我要个性网,获取头像

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'+div
    resp=requests.get(links,headers=headers)
    resp.encoding=resp.apparent_encoding
    htmls=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'%title
    for 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'+div
    resp=requests.get(links,headers=headers)
    resp.encoding=resp.apparent_encoding
    htmls=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'%title
    for 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!
安排一波

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值