Python爬蟲實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能示例
本文實(shí)例講述了Python爬蟲實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能。分享給大家供大家參考,具體如下:
這個(gè)是抓取信息的
from bs4 import BeautifulSoup import requests url = 'https://list.tmall.com/search_product.htm?q=%CB%AE%BA%F8+%C9%D5%CB%AE&type=p&vmarket=&spm=875.7931836%2FA.a2227oh.d100&from=mallfp..pc_1_searchbutton' response = requests.get(url) #解析網(wǎng)頁 soup = BeautifulSoup(response.text,'lxml') #.text將解析到的網(wǎng)頁可讀 storenames = soup.select('#J_ItemList > div > div > p.productTitle > a') #選擇出商店的信息 prices = soup.select('#J_ItemList > div > div > p.productPrice > em') #選擇出價(jià)格的信息 sales = soup.select('#J_ItemList > div > div > p.productStatus > span > em') #選擇出銷售額的信息 for storename, price, sale in zip(storenames,prices,sales): storename = storename.get_text().strip() #用get_text()方法篩選出標(biāo)簽中的文本信息,由于篩選結(jié)果有換行符\n所以用strip()將換行符去掉 price = price.get_text() sale = sale.get_text() print('商店名:%-40s價(jià)格:%-40s銷售額:%s'%(storename,price,sale)) #使打印出來的信息規(guī)范 print('----------------------------------------------------------------------------------------------')
這個(gè)是下載圖片的
from bs4 import BeautifulSoup import requests import urllib.request url = 'https://list.tmall.com/search_product.htm?q=%CB%AE%BA%F8+%C9%D5%CB%AE&type=p&vmarket=&spm=875.7931836%2FA.a2227oh.d100&from=mallfp..pc_1_searchbutton' response = requests.get(url) soup = BeautifulSoup(response.text, 'lxml') imgs = soup.select('#J_ItemList > div > div > div.productImg-wrap > a > img') a = 1 for i in imgs: if(i.get('src')==None): break img = 'http:'+i.get('src') #這里廢了好長的時(shí)間,原來網(wǎng)站必須要有http:的 #print(img) urllib.request.urlretrieve(img,'%s.jpg'%a, None,) a = a+1
ps:
1.選擇信息的時(shí)候用css
2.用get_text()
方法篩選出標(biāo)簽中的文本信息
3.strip
,lstrip
,rstrip
的用法:
Python中的strip
用于去除字符串的首尾字符;同理,lstrip
用于去除左邊的字符;rstrip
用于去除右邊的字符。
這三個(gè)函數(shù)都可傳入一個(gè)參數(shù),指定要去除的首尾字符。
需要注意的是,傳入的是一個(gè)字符數(shù)組,編譯器去除兩端所有相應(yīng)的字符,直到?jīng)]有匹配的字符,比如:
theString = 'saaaay yes no yaaaass' print theString.strip('say')
theString依次被去除首尾在['s','a','y']
數(shù)組內(nèi)的字符,直到字符在不數(shù)組內(nèi)。所以,輸出的結(jié)果為:
yes no
比較簡單吧,lstrip
和rstrip
原理是一樣的。
注意:當(dāng)沒有傳入?yún)?shù)時(shí),是默認(rèn)去除首尾空格和換行符的。
theString = 'saaaay yes no yaaaass' print theString.strip('say') print theString.strip('say ') #say后面有空格 print theString.lstrip('say') print theString.rstrip('say')
運(yùn)行結(jié)果:
yes no
es no
yes no yaaaass
saaaay yes no
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- Python中使用aiohttp模擬服務(wù)器出現(xiàn)錯(cuò)誤問題及解決方法
- Python requests及aiohttp速度對比代碼實(shí)例
- Python aiohttp百萬并發(fā)極限測試實(shí)例分析
- python aiohttp的使用詳解
- Python中利用aiohttp制作異步爬蟲及簡單應(yīng)用
- Python中asyncio與aiohttp入門教程
- Python爬蟲抓取指定網(wǎng)頁圖片代碼實(shí)例
- python requests抓取one推送文字和圖片代碼實(shí)例
- Python3簡單爬蟲抓取網(wǎng)頁圖片代碼實(shí)例
- Python使用爬蟲抓取美女圖片并保存到本地的方法【測試可用】
- Python爬蟲之網(wǎng)頁圖片抓取的方法
- python抓取網(wǎng)站的圖片并下載到本地的方法
- python 基于AioHttp 異步抓取火星圖片
相關(guān)文章
解決TensorFlow GPU版出現(xiàn)OOM錯(cuò)誤的問題
今天小編就為大家分享一篇解決TensorFlow GPU版出現(xiàn)OOM錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02解決pycharm上的jupyter notebook端口被占用問題
今天小編就為大家分享一篇解決pycharm上的jupyter notebook端口被占用問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python數(shù)據(jù)分析的八種處理缺失值方法詳解
缺失值可能是數(shù)據(jù)科學(xué)中最不受歡迎的值,然而,它們總是在身邊。忽略缺失值也是不合理的,因此我們需要找到有效且適當(dāng)?shù)靥幚硭鼈兊姆椒?/div> 2021-11-11如何使用python數(shù)據(jù)處理解決數(shù)據(jù)沖突和樣本的選取
這篇文章主要介紹了如何使用python數(shù)據(jù)處理解決數(shù)據(jù)沖突和樣本的選取,其中主要包括 實(shí)際業(yè)務(wù)數(shù)據(jù)沖突、樣本選取問題、數(shù)據(jù)共線性等思路2021-08-08最新評論