运用Python爬虫爬取一个美女网址,爬取美女图
  ZTR47FrQxTR9 2023年11月02日 42 0


运用Python爬虫爬取一个美女网址,爬取美女图

要运用到的python技术:

导入库

1.request 发送请求,从服务器获取数据

2.BeautifulSoup 用来解析整个网页的源代码

import requests
from bas4 import BeautifulSoup

爬取网站的第一步:发送请求到服务器

resp=requests.get("https://www.umei.cc/")#从服务器拿到源代码
改变编码格式为utf-8
resp.encoding("utf-8")

爬取的第二步:

运用导入bas4库解析html页面为第二步

#解析html
main_page=BeautifulSoup(resp.text,'html.parser')
#从页面中找到某些东西
#find()赵一个
#find_all()找所有
alst=main_page.findall("div",attrs={"class":"TypeList"}).findall("a",attrs={"class":"TypeBigPics"})

for a in alst:
print(a.get("href"))
#发送请求到子页面,进入到所有小姐姐的页面
href=a.get("href")
resp1=requests.get(href)
resp1.encoding('utf-8')
child_page=beautifulSoup(resp1.text)
child_page.findall("dive",attrs={"class":"ImageBody"}).find("img").get("src")

第三步,把爬取到的图片用文件打开,进行数据的持久化

#创建文件
f=open("tu_%s.jpg"%n,mode="wb")#wb表示写入的是非文本文件
f.write(requests.get(src).content)#向外拿出图片数据,不是文本信息
print("你已经成功下载了一个图片")
n+=1#n自增1

完整代码:

import requests
from bas4 import BeautifulSoup
resp=requests.get("https://www.umei.cc/")#从服务器拿到源代码
resp.encoding("utf-8")
#解析html
main_page=BeautifulSoup(resp.text,'html.parser')
#从页面中找到某些东西
#find()赵一个
#find_all()找所有
alst=main_page.findall("div",attrs={"class":"TypeList"}).findall("a",attrs={"class":"TypeBigPics"})

for a in alst:
print(a.get("href"))
#发送请求到子页面,进入到所有小姐姐的页面
href=a.get("href")
resp1=requests.get(href)
resp1.encoding('utf-8')
child_page=beautifulSoup(resp1.text)
child_page.findall("dive",attrs={"class":"ImageBody"}).find("img").get("src")
#发送请求
#创建文件
f=open("tu_%s.jpg"%n,mode="wb")#wb表示写入的是非文本文件
f.write(requests.get(src).content)#向外拿出图片数据,不是文本信息
print("你已经成功下载了一个图片")
n+=1#n自增1


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
ZTR47FrQxTR9
最新推荐 更多