我爬取的是原图,比页面的图片清楚一些,而且可以连续爬取多个页面的图片
效果图:
源代码:
import requests
import re
import os
from prettytable import PrettyTable
img_rule = re.compile(r'<p><a href="(.*?)" target="_blank" class="view_img_link"',re.S)
next_rule = re.compile(r'<div class="cp-pagenavi">\s*<a title="Newer Comments" href="(.*?)"',re.S)
previous_rule = re.compile(r'<a title="Older Comments" href="(.*?)"',re.S)
current_rule = re.compile(r'<span class="current-comment-page">.(\d\d).</span>',re.S)
def mk_dir():
if not os.path.exists('./jiandanLibs'):
os.mkdir('./jiandanLibs')
print('成功创建文件夹!!!')
def get_page(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
page_text = requests.get(url=url, headers=headers).text
semi_next = re.findall(next_rule, page_text)[0]
next_herf = 'http:' + semi_next
semi_previous = re.findall(previous_rule, page_text)[0]
previous_herf = 'http:' + semi_previous
current_page = re.findall(current_rule, page_text)[0]
next_page = int(current_page) + 1
previous_page = int(current_page) - 1
table = PrettyTable()
result = ["编码","当前页码","页码链接","操作"]
table.field_names = result
table.add_row([1,previous_page,previous_herf,"上一页"])
table.add_row([2,current_page,url,"当前页码"])
table.add_row([3,next_page,next_herf,"下一页"])
print(table)
while True:
try:
index = int(input(f'请输入您要进行的操作编码,如果想退出请输入4:'))
except:
print('格式错误,请输入[1-4]')
continue
else:
if index == 1:
get_pic(previous_herf)
elif index == 2:
get_pic(url)
elif index == 3:
get_pic(next_herf)
elif index == 4:
return
return
def get_pic(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
page_text = requests.get(url=url, headers=headers).text
img_src_list = re.findall(img_rule, page_text)
for src in img_src_list:
src = 'http:' + src
img_data = requests.get(url=src, headers=headers).content
img_name = src.split('/')[-1]
img_path = r'./jiandanLibs/' + img_name
with open(img_path, 'wb') as fp:
fp.write(img_data)
print(img_name, '下载成功!!!')
get_page(url)
if __name__ == "__main__":
mk_dir()
# 这里的url可以自己指定, headers自己定,加不加无所谓
get_page('http://jandan.net/girl/MjAyMjAyMDQtOTA=#comments')