python爬虫实战
一、写在前面
在本章中,我们将介绍怎么使用python作为工具来进行百度图库的图片的爬取。
二、需要的准备
我们需要使用的python库有:selenium、re、requests三个基本库。
2.1、库的安装
重点介绍一下selenium的安装:这里附上一个博主的链接,很详细
Selenium简介及使用案例
三、网页分析
首先,我们先打开百度图库,然后随便搜索一个关键词。然后按F12,查看网页的结构。
然后我们点击network按ctrl+r刷新界面,很明显百度图库是动态加载的。
我们向下滑动可以看见network有内容更新。此时,这一个网址引起了我们的注意。
我们通过preview查看它的返回值。
其中有我们的搜索关键词,我们再打开data,看见了里面有我们想要的关键信息-----hoverurl。
点开这个网址可以获得我们的图片。
为了进行批量的获得,我们观察网页的变化规则
发现每次只有pn=后面的数字发现了改变,每一个网页有30张图片。准备工作到这我们都弄完了,现在就直接上代码。
四、代码展示
一、获得图片的url
from selenium import webdriver
import re
import requests
filename = './data/leimu'
def get_url(i):
url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=7846733038692497737&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E2%80%98%E2%80%99%E8%95%BE%E5%A7%86%E2%80%98%E2%80%99&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=©right=&word=%E2%80%98%E2%80%99%E8%95%BE%E5%A7%86%E2%80%98%E2%80%99&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn='+ str(i) +'&rn=30&gsm=1e&1618215621934='
brower = webdriver.Chrome()
brower.get(url)
items = brower.find_elements_by_tag_name('strong')
result1 = []
for item in items:
text = item.text
results = re.findall('.*?"hoverURL":"(.*?)",.*?', text)
for result in results:
if result not in result1:
result1.append(result)
brower.close()
return result1
ps:在这,我们使用selenium来打开网页然后使用re来获得图片的url。
二、图片的保存
然后我们定义一个save_img的函数来保存图片。
def save_img(results, j):
for result in results:
try:
response = requests.get(result)
path = filename + '/' + str(j) + '.jpg'
j += 1
with open(path, 'wb') as f:
f.write(response.content)
f.close()
except requests.exceptions.MissingSchema:
pass
三、运行
if __name__ == '__main__':
j = -29
for i in range(60, 631, 30):
j+=29
results = get_url(i)
print('ok')
save_img(results, j)
ps:我们定义一个for循环来批量获得图片
五、全部代码
from selenium import webdriver
import re
import requests
filename = './data/leimu'
def get_url(i):
url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=7846733038692497737&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E2%80%98%E2%80%99%E8%95%BE%E5%A7%86%E2%80%98%E2%80%99&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=©right=&word=%E2%80%98%E2%80%99%E8%95%BE%E5%A7%86%E2%80%98%E2%80%99&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn='+ str(i) +'&rn=30&gsm=1e&1618215621934='
brower = webdriver.Chrome()
brower.get(url)
items = brower.find_elements_by_tag_name('strong')
result1 = []
for item in items:
text = item.text
results = re.findall('.*?"hoverURL":"(.*?)",.*?', text)
for result in results:
if result not in result1:
result1.append(result)
brower.close()
return result1
def save_img(results, j):
for result in results:
try:
response = requests.get(result)
path = filename + '/' + str(j) + '.jpg'
j += 1
with open(path, 'wb') as f:
f.write(response.content)
f.close()
except requests.exceptions.MissingSchema:
pass
if __name__ == '__main__':
j = -29
for i in range(60, 631, 30):
j+=29
results = get_url(i)
print('ok')
save_img(results, j)