python爬虫爬取网页壁纸图片(《底特律:变人》)

参考文章:https://www.cnblogs.com/franklv/p/6829387.html

爬虫爬取网址:http://www.gamersky.com/news/201804/1039678.shtml
此网页为静态网页,双层套图,无需登录
下面附上一个图片的两张套图的源码截图:
这里写图片描述
这里写图片描述
附上源码:

import requests
from bs4 import BeautifulSoup
import os
import re

def getHtmlurl(url):         #获取网址
    try:
       r=requests.get(url)
       r.raise_for_status()
       r.encoding=r.apparent_encoding
       return r.text
    except:
        return ""

def getpic(html): #获取图片地址并下载
    soup =BeautifulSoup(html,'html.parser')
    all_p=soup.find('div',class_='Mid2L_con').find_all("p",attrs={'align':'center'})
    for p in all_p:
       img_i=p.find('a')
       img_h=img_i['href']
       img_url=img_h.split('?')[-1]
       title=img_url.split('/')[-1].split('.')[0]
       print (img_url)

       root='/home/suwex/图片/'
       #root='/home/suwex/test2/'
       path = root + title[:22] + '.jpg'

       try:                              #创建或判断路径图片是否存在并下载
           if not os.path.exists(root):
               os.mkdir(root)
           if not os.path.exists(path):
               r = requests.get(img_url)
               with open(path, 'wb') as f:
                   f.write(r.content)
                   f.close()
                   print("文件保存成功")
           else:
               print("文件已存在")
       except:
           print("爬取失败")



def main():
    for i in range(1,12):
      if i==1:
        url='http://www.gamersky.com/news/201804/1039678.shtml'
      else:
        url='http://www.gamersky.com/news/201804/1039678_' + str(i) +'.shtml'
      html=(getHtmlurl(url))
      print(str(i)+" : ")
      print(getpic(html))
main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值