头歌故宫壁纸网页爬取实验

第1关:网页爬虫选择题

任务描述

本关任务: 完成四个选择题

DBBB

第2关:获取故宫壁纸网页的第一张图片的信息

任务描述

本关任务:编写一个获取故宫壁纸网页的第一张图片的信息。

import requests
from bs4 import   BeautifulSoup
url = "https://www.dpm.org.cn/lights/royal/p/1.html"
#代码开始
response = requests.get(url)
html_content = response.text

# 使用BeautifulSoup解析HTML内容
soup = BeautifulSoup(html_content, 'html.parser')

# 找到第一张图片的信息
pic=soup.find("div",class_="pic")
x=pic.find("img")
imglj=x.attrs['src']
imgmz=x.attrs['title'].strip()
#代码结束
print(imglj)
print(imgmz)
f1=open("tpxx.txt","w")
f1.write(imglj+"\n")
f1.write(imgmz+"\n")
f1.close()

第3关:下载故宫壁纸网页的第一张图片


任务描述

本关任务:下载故宫壁纸网页的第一张图片,网页地址为 https://www.dpm.org.cn/lights/royal/p/1.html ,并将图片文件命名为网页上图片的标题名,扩展名为 jpg ,将图片保存在 image 文件夹下。

import requests
from bs4 import   BeautifulSoup
url = "https://www.dpm.org.cn/lights/royal/p/1.html"
r=requests.get(url)
r.encoding = 'utf-8'
soup=BeautifulSoup(r.text,"html.parser")
pic=soup.find("div", class_="pic")
x=pic.find("img")
imglj=x.attrs["src"]
imgmz=x.attrs["title"].strip()
#代码开始
r=requests.get(imglj)
print(imgmz)
cpmc="image//"+imgmz+".jpg"
f1=open(cpmc,"bw")
f1.write(r.content)
f1.close()
#代码结束

第4关:下载故宫壁纸网页的单页的多张图片


任务描述

本关任务:下载故宫壁纸网页的多张图片,网页地址为 https://www.dpm.org.cn/lights/royal/p/1.html,并将图片文件命名为序号标题名,扩展名为 jpg ,将图片保存在 image 文件夹下。

import requests
from bs4 import   BeautifulSoup
url = "https://www.dpm.org.cn/lights/royal/p/1.html"
#代码开始
response = requests.get(url)
response.encoding="utf-8"
html_content = response.text
soup = BeautifulSoup(html_content, 'html.parser')
pics=soup.find_all("div",class_="pic")
i=0
for pic in pics:
    x=pic.find("img")
    imglj=x.attrs["src"]
    imgmz=x.attrs["title"].strip()
    # print(imgmz)
    i+=1
    r=requests.get(imglj)
    cpmc="image//"+str(i)+imgmz+".jpg"
    f1=open(cpmc,"bw")
    f1.write(r.content)
    f1.close()
#代码结束

第5关:获取故宫壁纸前五个网页的多张图片的信息

任务描述

本关任务:下载故宫壁纸前 5 个网页的多张图片,网页地址为 https://www.dpm.org.cn/lights/royal/p/1.htmlhttps://www.dpm.org.cn/lights/royal/p/5.html 。将图片文件命名为序号标题名,扩展名为 jpg ,将图片保存在 image 文件夹下。

import requests
from bs4 import   BeautifulSoup
#代码开始
i=0
for j in range(1,6):
    # print("j=",j)
    url = "https://www.dpm.org.cn/lights/royal/p/"+str(j)+".html"
    response = requests.get(url)
    response.encoding="utf-8"
    html_content = response.text
    soup = BeautifulSoup(html_content, 'html.parser')
    pics=soup.find_all("div",class_="pic")
    for pic in pics:
        x=pic.find("img")
        imglj=x.attrs["src"]
        imgmz=x.attrs["title"].strip()
        # print(imgmz)
        i+=1
        r=requests.get(imglj)
        cpmc="image//"+str(i)+imgmz+".jpg"
        f1=open(cpmc,"bw")
        f1.write(r.content)
        f1.close()
#代码结束

  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值