您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用python實現爬取圖書封面,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內容如下
kongfuzi.py
利用更換代理ip,延遲提交數據,設置請求頭破解網站的反爬蟲機制
import requests import random import time class DownLoad(): def __init__(self): self.ip_list = ['191.33.179.242:8080', '122.72.108.53:80', '93.190.142.214:80', '189.8.88.125:65301', '36.66.55.181:8080', '170.84.102.5:8080', '177.200.72.214:20183', '115.229.115.190:9000'] self.user_agent_list = [ 'User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50', 'User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11' ] def get(self, url, proxy=None, timeout=20, num=5): print("正在請求%s" % url) UA = random.choice(self.user_agent_list) headers = {'User-Agent': UA} if proxy == None: try: return requests.get(url, headers=headers, timeout=timeout) except: if num > 0: time.sleep(10) return self.get(url, num=num - 1) else: time.sleep(10) IP = ''.join(random.choice(self.ip_list).strip()) proxy = {'http': IP} return self.get(url, proxy=proxy, timeout=timeout) else: try: IP = ''.join(random.choice(self.ip_list).strip()) proxy = {'http': IP} return requests.get(url, headers=headers, proxy=proxy, timeout=timeout) except: if num > 0: time.sleep(10) IP = ''.join(random.choice(self.ip_list).strip()) proxy = {'http': IP} print("正在更換代理") print("當前代理%s" % proxy) return self.get(url, proxy=proxy, num=num - 1)
main.py
將爬取的圖片保存到本地,然后展示到界面
import kongfuzi import os import requests import bs4 from tkinter import * from PIL import Image, ImageTk # 下載圖片,生成圖片地址列表和圖書信息列表 def download(): baseUrl = "http://search.kongfz.com" keyword = e1.get() url = baseUrl + "/product_result/?select=0&key=" + keyword print("下載鏈接:" + url) show(url) # bs4處理 def changesoup(html): htm = html.content html_doc = str(htm, 'utf-8') soup = bs4.BeautifulSoup(html_doc, "html.parser") return soup # 圖書信息集合 def bookinfo(soup): # 圖書價格列表 price = [] soupprice = soup.select(".first-info .f_right .bold") for i in soupprice: price.append(i.string) # 書店名列表 storename = [] soupstorename = soup.select(".text a span") for each in soupstorename: if each.string == None: soupstorename.remove(each) for i in soupstorename: storename.append(i.string) # 商家地區列表 place = [] soupplace = soup.select(".user-place") for i in soupplace: place.append(i.string) # 書名列表 bookname = [] bookname1 = soup.select( ".search-wrap .search-main .search-main-result .result-content .result-list .item .item-info .title .link") # print(len(bookname1)) # print(bookname1) for each in bookname1: print(each) # a = bs4.BeautifulSoup(each, "html.parser") a = each.get_text() print(a) # type(a) # a = bs4.BeautifulSoup(a, "html.parser") # b = a.get_text() bookname.append(a) # print(bookname) # print(len(bookname)) return bookname, price, place, storename # 保存圖片 def imgsave(soup): dirName = "image" os.makedirs(dirName, exist_ok=True) filePathList = [] imgUrl = soup.select(".search-main-result .result-content .result-list .item .item-img .img-box img") # print(imgUrl) if not imgUrl: print("沒有找到當前節點下圖片") else: i = 0 for imageUrls in imgUrl: # 找到圖片地址 獲取它 downloadUrl = imageUrls.get('src') # if downloadUrl == "/searchfront/img/error.jpg": # downloadUrl = "https://cache.yisu.com/upload/information/20200622/113/50847.jpg" print("打印要下載的圖片地址:", downloadUrl) # http://book.kongfz.com/img/pc/error.jpg # 分割字符 split = downloadUrl.split("/") # 只保留最后一個元素 fileName = str(i) + "-" + os.path.basename(split[len(split) - 1]) print("文件名:" + fileName) # 建立一個新路徑 filePath = os.path.join(dirName, fileName) filePathList.append(filePath) if not os.path.exists(filePath): imageUrlPath = requests.get(downloadUrl) # 檢查當前網絡是否請求成功 imageUrlPath.raise_for_status() # 'wb'二進制模式打開img適用 imageFile = open(filePath, 'wb') for image in imageUrlPath.iter_content(10000): # 把每次遍歷的文件圖像都存儲進文件夾中 imageFile.write(image) # 關閉文件 imageFile.close() i = i + 1 return filePathList # 圖片展示 def show(url): xz = kongfuzi.DownLoad() html = xz.get(url) # 添加代理ip到ip_list add_ip = e2.get() xz.ip_list.append(add_ip) soup = changesoup(html) bookname, price, place, storename = bookinfo(soup) # print(bookname) # print(price) # print(place) # print(storename) filePathList = imgsave(soup) root1 = Toplevel() root1.geometry("1720x800") root1.title("孔網圖片爬取") # 處理圖片,轉換成可以顯示 photo = [] temp = [] for each in filePathList: temp = Image.open(each) photo.append(ImageTk.PhotoImage(temp)) canvas = Canvas(root1, width=1700, height=800, scrollregion=(0, 0, 0, 4000)) # 創建canvas canvas.place(x=10, y=10) # 放置canvas的位置 frame = Frame(canvas) # 把frame放在canvas里 frame.place(width=1680, height=800) for i in range(50): # 圖片行列 rownum = int(i / 5) columnnum = i % 5 # photo = ImageTk.PhotoImage(Image.open(filePathList[i])) imgLabel1 = Label(frame, image=photo[i], width=280, height=280) imgLabel1.grid(row=rownum * 5, column=columnnum, padx=10, pady=5) infoLabel1 = Label(frame, text="書名:" + bookname[i], bg="#FFF8DC", justify=LEFT) infoLabel1.grid(row=rownum * 5 + 1, column=columnnum, padx=45, pady=2, sticky=W) infoLabel2 = Label(frame, text="價格:" + price[i] + "元", bg="#FFF8DC", justify=LEFT) infoLabel2.grid(row=rownum * 5 + 2, column=columnnum, padx=45, pady=2, sticky=W) infoLabel3 = Label(frame, text="發貨地區:" + place[i], bg="#FFF8DC", justify=LEFT) infoLabel3.grid(row=rownum * 5 + 3, column=columnnum, padx=45, pady=2, sticky=W) infoLabel4 = Label(frame, text="書店:" + storename[i], bg="#FFF8DC", justify=LEFT) infoLabel4.grid(row=rownum * 5 + 4, column=columnnum, padx=45, pady=2, sticky=W) vbar = Scrollbar(canvas, orient=VERTICAL) # 豎直滾動條 vbar.place(x=1680, width=20, height=800) vbar.configure(command=canvas.yview) canvas.config(yscrollcommand=vbar.set) # 設置 canvas.create_window((800, 2000), window=frame) mainloop() if __name__ == '__main__': # 界面 root = Tk() root.title("孔網圖片爬取") e1 = Entry(root) e2 = Entry(root) e1.grid(row=0, column=0, padx=20, pady=20) e2.grid(row=0, column=2, padx=20, pady=20) label1 = Label(root, text="關鍵字", width=10).grid(row=0, column=1, padx=10, pady=5) label2 = Label(root, text="添加代理ip", width=10).grid(row=0, column=3, padx=10, pady=5) btn1 = Button(root, text="搜索", width=10, command=download).grid(row=1, column=1, padx=10, pady=5) # print(e1.get()) mainloop()
看完了這篇文章,相信你對“如何使用python實現爬取圖書封面”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。