鱼C论坛

 找回密码
 立即注册
查看: 847|回复: 7

如何爬虫获取网页中特定的图片

[复制链接]
发表于 2018-10-17 23:10:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
from bs4 import BeautifulSoup
import requests,os
def get_img(path,url,name):
    h    =  requests.get(url)
    name = '{0}.jpg'.format(name)
    img  =  os.path.join(path,name)
    f  =  open(img,'wb')
    f.write(h.content)
    f.close()
for key in range(1,14):
    url = 'http://www.1ppt.com/xiazai/ppt_xiazai_{0}.html'.format(key)
    html = requests.get(url)
    html.encoding = 'gbk'
    soup = BeautifulSoup(html.text)
    for i in soup.body.find('ul',class_="tplist").findAll('li'):
        name = i.find('img')['alt']
        listR = []
        for j in i.span.strings:
            listR.append(j)
        types = listR[0]
        num = listR[-1]      
        pic = i.find('img')['src']
        path = r'C:\Users\cxckb\Desktop\网页'
        picName = i.find('img')['alt']
        get_img(path,pic,picName)
        print([key,name,types,int(num.strip(':').strip('次'))])
   我想获取这个网站中下载次数最多的那个图片,想不出改用什么办法。。 所以想请教论坛里的大佬们。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-17 23:40:02 | 显示全部楼层
也想学习,不是站主如何知道哪个点击量高呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-18 01:41:18 | 显示全部楼层
找了半天怎么用BeautifulSoup 帮你写了个获取下载次数,和链接的方法,具体想要什么自己再调
  1. import requests
  2. from bs4 import BeautifulSoup


  3. def has_class_but_no_id(tag):
  4.     return tag.name == 'span' and  tag.contents[0].name == 'a'

  5. url = 'http://www.1ppt.com/xiazai/ppt_xiazai_1.html'
  6. r = requests.get(url)
  7. r.encoding = 'gb2312'
  8. html = r.text
  9. soup = BeautifulSoup(html, 'lxml')
  10. a = soup.find_all(has_class_but_no_id)

  11. for i in a:
  12.     print(i.contents)
  13.     print(i.contents[-1].strip(': 次'))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-18 08:37:06 | 显示全部楼层
  1. import requests
  2. from bs4 import BeautifulSoup


  3. def has_class_but_no_id(tag):
  4.     return tag.name == 'span' and  tag.contents[0].name == 'a'
  5. result=[]
  6. for key in range(1,14):
  7.     url = 'http://www.1ppt.com/xiazai/ppt_xiazai_{0}.html'.format(key)
  8.     r = requests.get(url)
  9.     r.encoding = 'gb2312'
  10.     html = r.text
  11.     soup = BeautifulSoup(html, 'lxml')
  12.     a = soup.find_all(has_class_but_no_id)
  13.     result=[]
  14.     for i in a:
  15.         result.append((i.parent.a.img['src'],i.contents[-1].strip(':次')))
  16.     result.sort(key=lambda x:str(x[1]),reverse=True)
  17.     result=result[:1]
  18. filename=result[0][0].split('/')[-1]
  19. im=requests.get(result[0][0])
  20. with open(filename,'wb') as f:
  21.     f.write(im.content)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-18 09:31:23 From FishC Mobile | 显示全部楼层
电脑不在身边,没法测试,
说一个需要注意的地方,
解析gbk编码的网站,少用bs4。
容易缺斤短两,用pq吧,解析式一样,写法稍微不一样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-18 13:30:17 | 显示全部楼层

谢谢! 我学习下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-18 13:31:29 | 显示全部楼层
RIXO 发表于 2018-10-18 01:41
找了半天怎么用BeautifulSoup 帮你写了个获取下载次数,和链接的方法,具体想要什么自己再调

十分感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-18 13:32:17 | 显示全部楼层
wongyusing 发表于 2018-10-18 09:31
电脑不在身边,没法测试,
说一个需要注意的地方,
解析gbk编码的网站,少用bs4。

好的,谢谢提醒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-4 02:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表