作者:zckun
链接:https://www.jianshu.com/p/e7d7616fa9d1
前言
有一段没用 python 了,我也不知道自己为什么对 python 越来越淡,可能自己还是比较喜欢 android ,毕竟自己第一次接触编程就是 android,为了android学java,然后接触的python,这次也是因为android,我要用一次python来帮我爬数据
正文
目标网站 https://divnil.com
看起来还挺清晰的,单击新窗口打开图片
然后下载图片,说实话,这图片很小,我很担心不是高清原图(管他的);
PS:一定要禁用广告拦截插件,不然加载不出图,我就在这被坑T_T;
接着分析我们从何入手
1、先去主页面获取每个图片的详细页面的链接
这链接还是比较好获取的,直接 F12 审核元素,或者右键查看代码,手机上chrome和firefox在url前面加上 "view-source"
比如: view-source:https://www.baidu.com/
2、从详细页面获取图片大图地址
随便打开一个图片详细页面如图:
接着按 F12 审核元素,我们需要定位该图片的链接,首先单击左上角的这玩意儿,像一个鼠标的图标:
接着只需要单击网页上的图片就能定位到代码了:
3、用大图地址下载该图片
这个很简单,看代码
import requestsfrom bs4 import BeautifulSoupimport sys
url = "https://divnil.com/wallpaper/iphone8/%E3%82%A2%E3%83%8B%E3%83%A1%E3%81%AE%E5%A3%81%E7%B4%99_2.html"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0",
}
resp = requests.get(url, headers=headers)
if resp.status_code != requests.codes.OK:
print("Request Error, Code: %d"% resp.status_code)
sys.exit()
soup = BeautifulSoup(resp.text, "html.parser")
contents = soup.findAll("div", id="contents")[0]
wallpapers = contents.findAll("a", rel="wallpaper")
links = []
for wallpaper in wallpapers:
links.append(wallpaper['href'])
import os
head = "https://divnil.com/wallpaper/iphone8/"
if os.path.exists("./Divnil") != True:
os.mkdir("./Divnil")
for url in links:
url = head + url
resp = requests.get(url, headers=headers)
if resp.status_code != requests.codes.OK:
print("URL: %s REQUESTS ERROR. CODE: %d" % (url, resp.status_code))
continue
soup = BeautifulSoup(resp.text, "html.parser")
img = soup.find("div", id="contents").contents.find("img", id="main_content")
img_url = head + img['"original'].replace("../", "")
img_name = img['alt']
print("start download %s ..." % img_url)
resp = requests.get(img_url, headers=headers)
if resp.status_code != requests.codes.OK:
print("IMAGE %s DOWNLOAD FAILED." % img_name)
with open("./Divnil/" + img_name + ".jpg", "wb") as f:
f.write(resp.content)
作者:zckun
链接:https://www.jianshu.com/p/e7d7616fa9d1
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
主要代码
完成,贴上所有代码
import requests
from bs4 import BeautifulSoup
import sys
import os
class Divnil:
def __init__(self):
self.url = "https://divnil.com/wallpaper/iphone8/%E3%82%A2%E3%83%8B%E3%83%A1%E3%81%AE%E5%A3%81%E7%B4%99.html"
self.head = "https://divnil.com/wallpaper/iphone8/"
self.headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0",
}
def getImageInfoUrl(self):
resp = requests.get(self.url, headers=self.headers)
if resp.status_code != requests.codes.OK:
print("Request Error, Code: %d"% resp.status_code)
sys.exit()
soup = BeautifulSoup(resp.text, "html.parser")
contents = soup.find("div", id="contents")
wallpapers = contents.findAll("a", rel="wallpaper"
)
self.links = []
for wallpaper in wallpapers:
self.links.append(wallpaper['href'])
def downloadImage(self):
if os.path.exists("./Divnil") != True:
os.mkdir("./Divnil")
for url in self.links:
url = self.head + url
resp = requests.get(url, headers=self.headers)
if resp.status_code != requests.codes.OK:
print("URL: %s REQUESTS ERROR. CODE: %d" % (url, resp.status_code))
continue
soup = BeautifulSoup(resp.text, "html.parser")
img = soup.find("div", id="contents").find("img", id="main_content")
img_url = self.head + img['original'].replace("../", "")
img_name = img['alt'
]
print("start download %s ..." % img_url)
resp = requests.get(img_url, headers=self.headers)
if resp.status_code != requests.codes.OK:
print("IMAGE %s DOWNLOAD FAILED." % img_name)
continue
if '/' in img_name:
img_name = img_name.split('/')[1]
with open("./Divnil/" + img_name + ".jpg", "wb") as f:
f.write(resp.content)
def main(self):
self.getImageInfoUrl()
self.downloadImage()
if __name__ == "__main__":
divnil = Divnil()
divnil.main()
结语
完整代码地址:https://github.com/ZCkun/divnilCrawler
感谢观看此文章 :)
学习Python就关注:datanami
近期文章:
推荐7个python入门视频教程
用Python几行代码教你画出太阳花
4年python,总结一些改善Python程序的建议
关于Python爬虫种类、法律、轮子的一二三
看完这篇文章如果还不能入门Python3就不能怪我了