鱼C论坛

 找回密码
 立即注册
查看: 2665|回复: 6

[技术交流] 爬取图片(没有加密)

[复制链接]
发表于 2020-6-10 08:59:13 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Cool_Breeze 于 2020-6-12 14:20 编辑
  1. #!/usr/bin/env python3
  2. #coding=utf-8

  3. from special_str_replace import special_str_replace
  4. import urllib.request,urllib.error
  5. from bs4 import BeautifulSoup as bfs
  6. import threading
  7. import os

  8. def main():
  9.     url = 'https://www.woyaogexing.com/touxiang/z/qlshouhui/'
  10.     home = 'https://www.woyaogexing.com'
  11.     html = gethtml(url)
  12.     for page_nu in get_page_list(html):
  13.         get_photo_url_list(gethtml(home + page_nu))

  14. def gethtml(url):
  15.     head = {
  16.     'Accept-Language': 'zh-CN,zh;q=0.9',
  17.     "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.30 Safari/537.36"
  18.     }
  19.    
  20.     req = urllib.request.Request(url=url, headers=head)
  21.     response = urllib.request.urlopen(req)
  22.     html = bfs(response,'html.parser') #解析html
  23.     # print(html)
  24.    
  25.     return html

  26. def get_page_list(html):
  27.     data = []
  28.     subject = html.find('div', class_="pMain")
  29.    
  30.     for i in subject.find_all('a', class_="img"):
  31.         data.append(i.attrs['href'])
  32.    
  33.     # print(data)
  34.     return data
  35.    
  36. def get_photo_url_list(html):
  37.     #<h1>....</h1>
  38.     title = str(html.find('h1').string).replace(':','_')
  39.     #替换字符串中的特殊字符为'_',为了创建文件夹
  40.     title = special_str_replace(title)
  41.     if not os.path.exists('./' + title):
  42.         os.mkdir(title)
  43.     os.chdir(title)
  44.     #ul class="artCont cl"
  45.     filterurl = html.find('ul', class_="artCont cl")
  46.     ph_url = []
  47.     for attr in filterurl.find_all('a'):
  48.         # print(attr.attrs)
  49.         ph_url.append(attr['href'])
  50.         
  51.     thread_photo(ph_url)
  52.     os.chdir('../') #返回文件夹
  53. def thread_photo(url):
  54.     thread = []
  55.     count = 0
  56.     for i in url:
  57.         count += 1
  58.         thread.append(threading.Thread(target=get_ptoto, args=(i,count)))
  59.     for i in thread:
  60.         i.start()
  61.     for i in thread:
  62.         i.join()
  63. def get_ptoto(u, count):
  64.     print(u, '===>', count, '.jpeg')
  65.     urllib.request.urlretrieve(\
  66.         'https:' + u,
  67.         str(count) + '.jpeg')
  68. if __name__ == '__main__':
  69.     main()
复制代码
  1. #!/usr/bin/env python3
  2. #coding=utf-8


  3. def special_str_replace(special):
  4.     limitstr = r'\/:*?"<>|'
  5.     test = list(special)
  6.     for index in range(len(test)):
  7.         if test[index] in limitstr:
  8.             test[index] = '_'
  9.             
  10.     return ''.join(test)
复制代码

结果

结果
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-10 16:48:00 | 显示全部楼层
不好看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-10 17:22:25 | 显示全部楼层

拿来练手的!图片都是一个表情,没有什么好看的!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-10 17:24:14 | 显示全部楼层
至今还不会自动登录网页,进行爬取。不知道有没有现成的教学
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-12 19:12:59 | 显示全部楼层
  1. #!/usr/bin/env python3
  2. #coding=utf-8

  3. from special_str_replace import special_str_replace
  4. import urllib.request,urllib.error
  5. from bs4 import BeautifulSoup as bfs
  6. import threading
  7. import os

  8. def main(page):
  9.     url = 'https://www.woyaogexing.com/touxiang/z/qlshouhui/' + page
  10.     home = 'https://www.woyaogexing.com'
  11.     html = gethtml(url)
  12.     for page_nu in get_page_list(html):
  13.         get_photo_url_list(gethtml(home + page_nu))

  14. def gethtml(url):
  15.     head = {
  16.     'Accept-Language': 'zh-CN,zh;q=0.9',
  17.     "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.30 Safari/537.36"
  18.     }
  19.    
  20.     req = urllib.request.Request(url=url, headers=head)
  21.     response = urllib.request.urlopen(req)
  22.     html = bfs(response,'html.parser') #解析html
  23.     # print(html)
  24.    
  25.     return html

  26. def get_page_list(html):
  27.     data = []
  28.     subject = html.find('div', class_="pMain")
  29.    
  30.     for i in subject.find_all('a', class_="img"):
  31.         data.append(i.attrs['href'])
  32.    
  33.     # print(data)
  34.     return data
  35.    
  36. def get_photo_url_list(html):
  37.     #<h1>....</h1>
  38.     title = str(html.find('h1').string).replace(':','_')
  39.     #替换字符串中的特殊字符为'_',为了创建文件夹
  40.     title = special_str_replace(title)
  41.     if not os.path.exists('./' + title):
  42.         os.mkdir(title)
  43.     os.chdir(title)
  44.     #ul class="artCont cl"
  45.     filterurl = html.find('ul', class_="artCont cl")
  46.     ph_url = []
  47.     for attr in filterurl.find_all('a'):
  48.         # print(attr.attrs)
  49.         ph_url.append(attr['href'])
  50.         
  51.     thread_photo(ph_url)
  52.     os.chdir('../') #返回文件夹
  53. def thread_photo(url):
  54.     thread = []
  55.     count = 0
  56.     for i in url:
  57.         count += 1
  58.         thread.append(threading.Thread(target=get_ptoto, args=(i,count)))
  59.     for i in thread:
  60.         i.start()
  61.     for i in thread:
  62.         i.join()
  63. def get_ptoto(u, count):
  64.     print(u, '===>', count, '.jpeg')
  65.     urllib.request.urlretrieve(\
  66.         'https:' + u,
  67.         str(count) + '.jpeg')
  68. if __name__ == '__main__':
  69.     for i in range(2,9):
  70.         main('index_' + str(i) +'.html')
复制代码
QQ浏览器截图20200612191107.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-22 10:25:46 | 显示全部楼层
如何获取一个网页的所有图片呢  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-31 07:35:38 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 19:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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