baidu

作者: 章鱼杂咀 | 来源:发表于2017-12-21 19:38 被阅读0次

图像简单处理(PIL or Pillow)

Python拼接图片

'''

--coding:utf-8--

import re,requests
from bs4 import BeautifulSoup

headers = {
# 'Host': 'i.meizitu.net',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Connection': 'keep-alive',
'Accept-Encoding':'gzip, deflate',
'Upgrade-Insecure-Requests':'1'
} # 请求头

s = requests.session() # 保留会话

def re_test(text):
'''
:param text: 网页源文件
:return: 返回图片的链接
'''
# https://imgsa.baidu.com/forum/w%3D580/sign=20bec30aa0ec8a13141a57e8c7029157/2508562c11dfa9ecfd81b1f26bd0f703938fc180.jpg
img_url = re.findall('https://imgsa.baidu.com/forum/.*?jpg',text)
return img_url

def bs_test(text):
'''
:param text: 网页源文件
:return: 返回图片的链接
'''
# <img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=44312fe1a5af2eddd4f149e1bd110102/1c3477094b36acaf1ce5c71b75d98d1000e99c2f.jpg" size="201260" width="479" height="852">
soup = BeautifulSoup(text, "lxml")
img_urls = soup.find_all('img',{'class':'BDE_Image'})
img_url = [i.get('src') for i in img_urls]
return img_url

def img_size(content):
# 熟悉下面这个图片处理库,对于验证码处理和AI有很大帮助哦。
from PIL import Image
from io import BytesIO
img = Image.open(BytesIO(content))
# width,height = img.size # 获取图片大小,更改图片大小,拼接照片墙自己先试试
return img.size

def save_img(url):
'''
:param url: 图片地址
:return: 木有返回值
'''
img_name = url.strip().split('/')[-1]
print(img_name)
url_re = s.get(url.strip(),headers=headers)
if url_re.status_code == 200: # 200是http响应状态
# print('准备保存')
import os
if not os.path.exists('baidu_img'): # 没有文件夹,则创建文件夹
os.mkdir('baidu_img')
if img_size(url_re.content)[0] > 400 and img_size(url_re.content)[1] > 600: # 图片宽高大于400600像素才保存
print('尺寸不错,留下了')
open('baidu_img/' + img_name, 'wb').write(url_re.content)

if name == 'main':
for i in range(2) : # 用2页测试一下
url = 'https://tieba.baidu.com/p/5033202671?pn='+str(i+1) # 构造和Page相关的链接
req_text = s.get(url).text
#print(re_test(req_text)) # 正则
# urls = re_test(req_text)
# print(bs_test(req_text)) # BS
urls = bs_test(req_text)
for img_url in re_test(req_text): # 采用正则获取图片链接
save_img(img_url)
'''

相关文章

  • baidu

    图像简单处理(PIL or Pillow) Python拼接图片 ''' --coding:utf-8-- imp...

  • baidu AI

  • practice at baidu

    今日实习,总结出几条比较好的工作习惯,以飨观众: 1.要尽快支持他人,避免脱掉团队后腿,这点在团队协作中很重要 2...

  • Apollo in Baidu

    无人驾驶概览 发展历程 2017年,百度发布了名为 Apollo 的开源无人驾驶项目,该平台帮助合作伙伴整合自有车...

  • Baidu AIOps实践

  • download baidu resources

    装好后刷新页面选中要下载的文件就看到啦 实际上还可以用github上面的baidu-pcs项目,这是一个命令行工具...

  • Baidu文库下载

    转载 Baidu文库下载 1、南瓜下载网 http://www.588230.com/ 这个很坚挺了,一直免费的,...

  • BAIDU api 实例

    http://www.huhangfei.com/program/javascript-jquery-440.html

  • baidu-template

    http://wangxiao.github.io/BaiduTemplate/

  • Another Accomplice with Baidu

    你讨厌百度吗? 如果直接问这个问题,恐怕不少的人都要说讨厌。 那么你是如何讨厌他的? 有没有为了方便、效率、以及冠...

网友评论

      本文标题:baidu

      本文链接:https://www.haomeiwen.com/subject/fdfzwxtx.html