爬虫爬取链接中文字_使用爬虫技术爬取图片链接并下载图片
生活随笔
收集整理的這篇文章主要介紹了
爬虫爬取链接中文字_使用爬虫技术爬取图片链接并下载图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取圖片比獲取文字更加復雜,獲取文字在網頁當中可以直接一次性的讀取到文字;獲取圖片是獲取圖片的鏈接地址,然后通過鏈接地址下載到本地。
第一步:如何獲取圖片的鏈接地址
打開圖片新聞的地址:https://www.infoq.com/presentations
1.?先獲取圖片地址:右鍵-查看源代碼,獲取新聞可以用文字的關鍵字搜索,作為圖片在網頁嵌入肯定會有img這樣的標簽,img后面跟著的鏈接地址就是我們要找的圖片鏈接地址。
2.代碼實現
import osimport shutilfrom bs4 import BeautifulSoup # 編碼解碼庫import requests # 請求訪問庫# 用 dict 定義http頭,偽裝瀏覽器訪問,避免被拒之門外headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Language": "zh-CN,zh;q=0.8", "Connection": "close", "Cookie": "_gauges_unique_hour=1; _gauges_unique_day=1; _gauges_unique_month=1; _gauges_unique_year=1; _gauges_unique=1", "Referer": "https://www.infoq.com/presentations", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER"}url='https://www.infoq.com/presentations'def download_pic(img_url,image_localpath): response=requests.get(img_url,stream=True) if response.status_code==200: with open (image_localpath,'wb') as f: response.raw.deconde_content=True shutil.copyfileobj(response.raw,f)#shutil庫用來配合requests模塊,他把response返回的內容寫入到文件中#獲取演講圖片,requests庫封裝的接口只提供了瀏覽的功能,如果想要下載的話需要提供stream這樣的參數,stream設置成true才能和網頁建立一個鏈接,再用response.content這樣的方法進行下載def craw3(url): response=requests.get(url,headers=headers) soup=BeautifulSoup(response.text,'lxml') for pic_href in soup.find_all('div',class_='card__content'): for pic in pic_href.find_all('img'): imgurl=pic.get('src') dir=os.path.join('.') filename=os.path.basename(imgurl)#該功能將鏈接地址只獲取圖片名稱 imgpath=os.path.join(dir,filename) #http://a.com/b/c.png只獲取c.png文件名稱 #c.png在和路徑結合 print('開始下載 %s'%imgurl) download_pic(imgurl,imgpath)craw3(url)注意:
requests庫封裝的接口只提供了瀏覽的功能,如果想要下載的話需要提供stream這樣的參數,stream設置成true才能和網頁建立一個鏈接,再用response.content這樣的方法進行下載
3.翻頁功能
import osimport shutilfrom bs4 import BeautifulSoup # 編碼解碼庫import requests # 請求訪問庫# 用 dict 定義http頭,偽裝瀏覽器訪問,避免被拒之門外headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Language": "zh-CN,zh;q=0.8", "Connection": "close", "Cookie": "_gauges_unique_hour=1; _gauges_unique_day=1; _gauges_unique_month=1; _gauges_unique_year=1; _gauges_unique=1", "Referer": "https://www.infoq.com/presentations", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER"}url='https://www.infoq.com/presentations'def download_pic(img_url,image_localpath): response=requests.get(img_url,stream=True) if response.status_code==200: with open (image_localpath,'wb') as f: response.raw.deconde_content=True shutil.copyfileobj(response.raw,f)#shutil庫用來配合requests模塊,他把response返回的內容寫入到文件中#獲取演講圖片,requests庫封裝的接口只提供了瀏覽的功能,如果想要下載的話需要提供stream這樣的參數,stream設置成true才能和網頁建立一個鏈接,再用response.content這樣的方法進行下載def craw3(url): response=requests.get(url,headers=headers) soup=BeautifulSoup(response.text,'lxml') for pic_href in soup.find_all('div',class_='card__content'): for pic in pic_href.find_all('img'): imgurl=pic.get('src') dir=os.path.join('.') filename=os.path.basename(imgurl)#該功能將鏈接地址只獲取圖片名稱 imgpath=os.path.join(dir,filename) #http://a.com/b/c.png只獲取c.png文件名稱 #c.png在和路徑結合 print('開始下載 %s'%imgurl) download_pic(imgurl,imgpath)#craw3(url)j=0for i in range(12,46,12): url='https://www.infoq.com/presentations'+str(i) j+=1 print('第%d頁'%j)craw3(url)問題
考慮多線程實現
做夢夢到了海豚在跳躍
總結
以上是生活随笔為你收集整理的爬虫爬取链接中文字_使用爬虫技术爬取图片链接并下载图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 节点图一般的比例_基于图的异常检测(二)
- 下一篇: bootstrap获取表格中选中行的值_