美图录爬虫(requests模块,re模块)
Python
爬虫
最近学正则表达式,刚好知道这个网站美图录,就做了个爬虫拿来练练手,说一说遇到的问题
一 404问题
问题:
由于图片显示页面是分页的,每一页展示5张图片,为了方便没有每次去获取下一页链接,而是使用了拼接字符串的形式,本以为遇到不存在的页面会抛出异常,测试了下,结果当网站遇到404时会直接跳转推荐页,于是requests还能正常返回数据
解决方法:
requests提供了一个req_object.status_code参数,用于返回页面状态码,判断一下这个参数为404就停止生成链接
二 文件写入问题
问题:
写入图片文件,图片无法正常显示
解决方法:
requests提供了img.content参数,用于将接受到的信息转换为二进制,然后在文件写入时,写入模式为"wb",以二进制方式写入
三反爬策略
问题:
爬取图片时发现图片会返回403(禁止访问资源),原因是网站做了防盗链处理,非本站访问图片都会返回403
解决方法:
在获取图片时添加headers请求头,在请求头中添加
'Referer':'https://m.meitulu.com/item/1.html'
将Referer 值设置为本站的图片展示页链接,这样服务器会认为请求来自本站页面,返回信息就会正常
美图录爬虫
# -*- coding: utf-8 -*-
import requests
import re
import os
def url_ls(url):
#获取页面所有套图链接
html=requests.get(url)
html.encoding=html.apparent_encoding
url_ls=re.findall("https\:\/\/m\.meitulu\.com\/item\/\w+",html.text)
return url_ls
def tup(url):
#获取图片链接及套图名
url_ls=[]
num=1
name=""
while True:
if num==1:
url1=url+".html"
num+=1
print(name)
else:
url1=url+"_{}.html".format(num)
num+=1
print(url1)
a=requests.get(url1)
if a.status_code==404:
break
a.encoding=a.apparent_encoding
my_url=re.findall("https\:\/\/mtl\.ttsqgs\.com\/images\/img\/\w+\/\w+\.jpg",a.text)
if name=="":
na=re.findall("\<h1\>[\w\W]+\<\/h1\>",a.text)
nam=re.sub("\<h1\>","",na[0])
name=re.sub("\<\/h1\>","",nam)
print(name)
for i in my_url:
if "/0.jpg" not in i:
url_ls.append(i)
return url_ls,name
def w(url,name,num):
headers={
'Host':'mtl.ttsqgs.com',
'Connection':'keep-alive',
'User-Agent':'Mozilla/5.0 (Linux; Android 7.1.2; M6 Note) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.105 Mobile Safari/537.36',
'Accept':'image/webp,image/apng,image/*,*/*;q=0.8',
'Referer':'https://m.meitulu.com/item/1.html',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.9'
}
name=re.sub('[\/:*?"<>|]',"_",name)
print(name)
print("正在下载{}".format(name))
img=requests.get(url,headers=headers)
os.system("clear")
imga=img.content
with open("./{}/{}.jpg".format(name,num),"wb") as f:
print("正在写入{}".format(num))
f.write(imga)
f.close()
url=input("url")
urllist=url_ls(url)
for i in set(urllist):
tup_ls,name=tup(i)
os.makedirs("./{}".format(name))
n=0
for j in tup_ls:
n+=1
num='{:0>4}'.format(n)
w(j,name,num)
美图录爬虫(requests模块,re模块)的更多相关文章
- 爬虫--requests模块学习
requests模块 - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能 ...
- Python开发基础-Day15正则表达式爬虫应用,configparser模块和subprocess模块
正则表达式爬虫应用(校花网) import requests import re import json #定义函数返回网页的字符串信息 def getPage_str(url): page_stri ...
- python基础之正则表达式爬虫应用,configparser模块和subprocess模块
正则表达式爬虫应用(校花网) 1 import requests 2 import re 3 import json 4 #定义函数返回网页的字符串信息 5 def getPage_str(url): ...
- Python基础之模块:5、 第三方模块 requests模块 openpyxl模块
目录 一.第三方模块的下载与使用 1.什么是第三方模块 2.如何安装第三方模块 方式一:pip工具 方式二:pycharm中下载 3.注意事项 1.报错并有警告信息 2.报错,提示关键字 3.报错,无 ...
- 【爬虫入门手记03】爬虫解析利器beautifulSoup模块的基本应用
[爬虫入门手记03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.Bea ...
- Python爬虫与数据分析之模块:内置模块、开源模块、自定义模块
专栏目录: Python爬虫与数据分析之python教学视频.python源码分享,python Python爬虫与数据分析之基础教程:Python的语法.字典.元组.列表 Python爬虫与数据分析 ...
- 【网络爬虫入门03】爬虫解析利器beautifulSoup模块的基本应用
[网络爬虫入门03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.B ...
- 爬虫(三):urllib模块
1. urllib模块 1.1 urllib简介 urllib 是 Python3 中自带的 HTTP 请求库,无需复杂的安装过程即可正常使用,十分适合爬虫入门 urllib 中包含四个模块,分别是: ...
- python day 8: re模块补充,导入模块,hashlib模块,字符串格式化,模块知识拾遗,requests模块初识
目录 python day 8 1. re模块补充 2. import模块导入 3. os模块 4. hashlib模块 5. 字符串格式:百分号法与format方法 6. 模块知识拾遗 7. req ...
随机推荐
- CSS 检测 IE 浏览器
CSS 检测 IE 浏览器 <!--[if IE]> <link href="ie.css" rel="stylesheet"> < ...
- Parcel all in one
Parcel all in one Parcel https://parceljs.org/ # cli $ yarn global add parcel-bundler $ npm install ...
- convert image to base64 in javascript
convert image to base64 in javascript "use strict"; /** * * @author xgqfrms * @license MIT ...
- React Native Apps
React Native Apps https://github.com/ReactNativeNews/React-Native-Apps github app https://github.com ...
- js & sort array object
js & sort array object sort array object in js https://flaviocopes.com/how-to-sort-array-of-obje ...
- Dart & basic
Dart & basic 2.4.0 Dart SDK https://github.com/dart-lang/sdk/releases https://github.com/dart-la ...
- PAUL ADAMS ARCHITECT:华州加州城市楼市竞争力居全美前列
美国房地产公司最新一份报告显示,美国楼市竞争力最高的十个城市中有八个来自华盛顿州和加利福尼亚州,主要原因在于越来越多的买家考虑迁居房价更划算的都会区.数据显示,斯潘那维65%的房屋以高于要价的价格售出 ...
- MySQL的简单使用方法备忘
这只是一篇我的个人备忘录,写的是我常用的命令.具体可以参考"菜鸟教程" https://www.runoob.com/mysql/mysql-tutorial.html 登录(用户 ...
- [转]ROS 传感器消息及RVIZ可视化Laserscan和PointCloud
https://blog.csdn.net/yangziluomu/article/details/79576508 https://answers.ros.org/question/60239/ho ...
- 微信小程序:将yyyy-mm-dd格式的日期转换成yyyy-mm-dd hh:mm:ss格式的日期
代码如下: changeDate1(e) { console.log(e); var date = new Date(e.detail.value); console.log(date); const ...