Python爬虫练习 爬取图片

virtualenv: 是针对python的包的多版本管理,通过将python包安装到一个模块来作为python的包虚拟环境,通过切换目录来实现不同包环境间的切换。其实不是非常爱捣腾的话,此工具就已经可以应付同python版本多环境问题了,安装使用也非常简单

1、请求网页

import requests
import re
import os
import time

response = requests.get("https://www.vmgirls.com/13970.html")
print(response.request.headers)
print(response.text)

requests1没有请求头会被反爬,默认的会被反爬,这里是默认的爬虫的user-agent

{'User-Agent': 'python-requests/2.24.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>XCat</center>
</body>
</html>

获取请求头 user-agent用户代理,告诉服务器请求者的身份

headers = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
             'Chrome/83.0.4103.106 Safari/537.36'
}

response = requests.get("https://www.vmgirls.com/13970.html", headers = headers)

方式:在网页检查源代码中,找到network,刷新网页,一般在第一个是网页的地址,查看该地址,在最后一行出现user-agent

补充:

import requests
 
response  = requests.get("https://www.baidu.com")
print(type(response))
print(response.status_code)
print(type(response.text))
 
response.enconding = "utf-8'
print(response.text)
 
print(response.cookies)
 
print(response.content)
print(response.content.decode("utf-8"))

response.text返回的是Unicode格式,通常需要转换为utf-8格式,否则就是乱码。response.content是二进制模式,可以下载视频之类的,如果想看的话需要decode成utf-8格式。
  不管是通过response.content.decode("utf-8)的方式还是通过response.encoding="utf-8"的方式都可以避免乱码的问题发生

2、解析网页

这里使用正则表达式2来进行处理,在繁杂的网页代码中提取自己想要的部分、还有bs4

xpath3功能更强大

.*?匹配除了换行符之间的所有内容

# 创建文件名,先找到名字,如果有名字就写进去,如果没有创建一个
dir_name = re.findall('<h1 class="post-title h3">(.*?)</h1>', html)[-1]
if not os.path.exists(dir_name):
    os.mkdir(dir_name)
urls = re.findall('<a href="(.*?)" alt=".*?" title=".*?">', html)
print(urls)

3、保存图片

for url in urls:
    time.sleep(1) #为了减轻对面网站服务器的压力 
    # 图片的名字
    files_name = url.split('/')[-1]
    response = requests.get(url, headers = headers)
    #写进相对目录夹中
    with open(dir_name + '/' + files_name, 'wb',) as f:
        f.write(response.content)

这里涉及os4的知识

4、完整代码(比较弱小爬取能力有限)

import requests
import re
import os
import time
headers = {
 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
             'Chrome/83.0.4103.106 Safari/537.36'
}

response = requests.get("https://www.vmgirls.com/13888.html", headers = headers)
html = response.text

'''解析网页 遍历列表,保存'''
# 创建文件名,先找到名字,如果有名字就写进去,如果没有创建一个
dir_name = re.findall('<h1 class="post-title h3">(.*?)</h1>', html)[-1]
if not os.path.exists(dir_name):
    os.mkdir(dir_name)
urls = re.findall('<a href="(.*?)" alt=".*?" title=".*?">', html)
print(urls)

'''保存图片'''
for url in urls:
    time.sleep(1)
    # 图片的名字
    files_name = url.split('/')[-1]
    response = requests.get(url, headers = headers)
    with open(dir_name + '/' + files_name, 'wb',) as f:
        f.write(response.content)

  1. Requests: 让 HTTP 服务人类 ↩︎

  2. 正则表达式 ↩︎

  3. xpath相关教程 w3schoolxpath教程 ↩︎

  4. Python OS 文件/目录方法 ↩︎

猜你喜欢

转载自blog.csdn.net/Xiaowang98/article/details/106876232
今日推荐