qt之佈局管理器
阿新 • • 發佈:2021-01-08
此文轉載自:https://blog.csdn.net/qq_52907353/article/details/112391518#commentBox
今天要寫的是爬取百度圖片
一、分析過程
1.首先,開啟百度,然後開啟我們的抓包工具 然後搜尋一個內容,點選圖片
2.之前用滑鼠滾輪往下滑的過程中,發現圖片是動態加載出來的,也就說明這是一個ajax請求。
有了這些思路,開啟我們的抓包工具
3.選擇XHR選項
4.然後滑鼠滾輪往下拖動,我們會發現一個數據包。
5.複製這個資料包的URL請求
https://image.baidu.com/search/acjson?tn=resultjson_com&logid=8222346496549682679&ipn=rj&ct=201326592&is=& fp=result&queryWord=%E7%BE%8E%E5%A5%B3&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=©right=&word=%E7%BE%8E%E5%A5%B3&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=& force=&cg=girl&pn=30&rn=30&gsm=1e&1610176483429=
6.點開這個URL看到其攜帶的引數
然後也複製一下
開始編寫相關程式碼
二、編寫程式碼
1.首先引入我們所需要的模組
import requests
2.開始程式碼編寫
#進行UA偽裝
header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
url = 'https://image.baidu.com/search/acjson?'
param = {
'tn': 'resultjson_com',
'logid': '8846269338939606587',
'ipn': 'rj',
'ct': '201326592',
'is': '',
'fp': 'result',
'queryWord': '美女',
'cl': '2',
'lm': '-1',
'ie': 'utf-8',
'oe': 'utf-8',
'adpicid': '',
'st': '-1',
'z':'' ,
'ic':'' ,
'hd': '',
'latest': '',
'copyright': '',
'word': '美女',
's':'' ,
'se':'' ,
'tab': '',
'width': '',
'height': '',
'face': '0',
'istype': '2',
'qc': '',
'nc': '1',
'fr': '',
'expermode': '',
'force': '',
'cg': 'girl',
'pn': '1',
'rn': '30',
'gsm': '1e',
}
#將編碼形式轉換為utf-8
page_text = requests.get(url=url,headers=header,params=param)
page_text.encoding = 'utf-8'
page_text = page_text.text
print(page_text)
到了這一步,我們先訪問一下,看看能不能取得到頁面返回的資料
我們成功取得了返回資料
之後我們返回到網頁中,檢視資料包,檢視他的返回資料
然後將其放入json線上解析工具中發現了圖片所對應的地址
之後繼續編寫程式碼
將返回資料轉換為json格式,而且發現數據全部儲存在一個字典當中,並且圖片的地址也在一個字典當中
然後將連結地址取出
page_text = page_text.json()
#先取出所有連結所在的字典,並將其儲存在一個列表當中
info_list = page_text['data']
#由於利用此方式取出的字典最後一個為空,所以刪除列表中最後一個元素
del info_list[-1]
#定義一個儲存圖片地址的列表
img_path_list = []
for info in info_list:
img_path_list.append(info['thumbURL'])
#再將所有的圖片地址取出,進行下載
#n將作為圖片的名字
n = 0
for img_path in img_path_list:
img_data = requests.get(url=img_path,headers=header).content
img_path = './' + str(n) + '.jpg'
with open(img_path,'wb') as fp:
fp.write(img_data)
n += 1
在完成這些以後,我們還想要實現百度圖片下載多頁,經過分析,我發現在我們提交的引數裡,pn代表的是從第幾張圖片開始載入,順著這個思路我們可以給上面的程式碼套一個大迴圈,即第一次下載從第1張開始,下載三十張,第二次從第31張開始下載。
OK!思路已經明確,開始修改上面的程式碼
import requests
from lxml import etree
page = input('請輸入要爬取多少頁:')
page = int(page) + 1
header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
n = 0
pn = 1
#pn是從第幾張圖片獲取 百度圖片下滑時預設一次性顯示30張
for m in range(1,page):
url = 'https://image.baidu.com/search/acjson?'
param = {
'tn': 'resultjson_com',
'logid': '8846269338939606587',
'ipn': 'rj',
'ct': '201326592',
'is': '',
'fp': 'result',
'queryWord': '美女',
'cl': '2',
'lm': '-1',
'ie': 'utf-8',
'oe': 'utf-8',
'adpicid': '',
'st': '-1',
'z':'' ,
'ic':'' ,
'hd': '',
'latest': '',
'copyright': '',
'word': '美女',
's':'' ,
'se':'' ,
'tab': '',
'width': '',
'height': '',
'face': '0',
'istype': '2',
'qc': '',
'nc': '1',
'fr': '',
'expermode': '',
'force': '',
'cg': 'girl',
'pn': pn,#從第幾張圖片開始
'rn': '30',
'gsm': '1e',
}
page_text = requests.get(url=url,headers=header,params=param)
page_text.encoding = 'utf-8'
page_text = page_text.json()
info_list = page_text['data']
del info_list[-1]
img_path_list = []
for i in info_list:
img_path_list.append(i['thumbURL'])
for img_path in img_path_list:
img_data = requests.get(url=img_path,headers=header).content
img_path = './' + str(n) + '.jpg'
with open(img_path,'wb') as fp:
fp.write(img_data)
n = n + 1
pn += 29
如果感覺我寫的還可以就請點個贊把,如果有錯誤希望指出,我會積極改正!