Python爬蟲(chóng)實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能示例
本文實(shí)例講述了Python爬蟲(chóng)實(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)頁(yè)
soup = BeautifulSoup(response.text,'lxml') #.text將解析到的網(wǎng)頁(yè)可讀
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)) #使打印出來(lái)的信息規(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') #這里廢了好長(zhǎng)的時(shí)間,原來(lái)網(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
比較簡(jiǎn)單吧,lstrip和rstrip原理是一樣的。
注意:當(dāng)沒(méi)有傳入?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文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python中使用aiohttp模擬服務(wù)器出現(xiàn)錯(cuò)誤問(wèn)題及解決方法
- Python requests及aiohttp速度對(duì)比代碼實(shí)例
- Python aiohttp百萬(wàn)并發(fā)極限測(cè)試實(shí)例分析
- python aiohttp的使用詳解
- Python中利用aiohttp制作異步爬蟲(chóng)及簡(jiǎn)單應(yīng)用
- Python中asyncio與aiohttp入門教程
- Python爬蟲(chóng)抓取指定網(wǎng)頁(yè)圖片代碼實(shí)例
- python requests抓取one推送文字和圖片代碼實(shí)例
- Python3簡(jiǎn)單爬蟲(chóng)抓取網(wǎng)頁(yè)圖片代碼實(shí)例
- Python使用爬蟲(chóng)抓取美女圖片并保存到本地的方法【測(cè)試可用】
- Python爬蟲(chóng)之網(wǎng)頁(yè)圖片抓取的方法
- python抓取網(wǎng)站的圖片并下載到本地的方法
- python 基于AioHttp 異步抓取火星圖片
相關(guān)文章
Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的的棧隊(duì)列
這篇文章主要介紹了Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的的棧,隊(duì)列,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
win10下python3.5.2和tensorflow安裝環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了win10下python3.5.2和tensorflow安裝環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Python腳本如何在bilibili中查找彈幕發(fā)送者
這篇文章主要介紹了如何在bilibili中查找彈幕發(fā)送者,本文給大家分享小編寫的一個(gè)python腳本來(lái)實(shí)現(xiàn)bilibili彈幕發(fā)送者,需要的朋友可以參考下2020-06-06
python學(xué)習(xí)教程之socket庫(kù)的基本使用(網(wǎng)絡(luò)編程-套接字)
Python中的socket模塊提供了網(wǎng)絡(luò)編程中的套接字(socket)功能,通過(guò)套接字我們可以實(shí)現(xiàn)不同計(jì)算機(jī)之間的通信,這篇文章主要給大家介紹了關(guān)于python學(xué)習(xí)教程之socket庫(kù)的基本使用,需要的朋友可以參考下2024-07-07
pycharm2023.1配置python解釋器時(shí)找不到conda環(huán)境解決辦法
如果你已經(jīng)安裝了Anaconda或Miniconda,但是在PyCharm中找不到conda解釋器,可以試試本文介紹的方法,這篇文章主要給大家介紹了關(guān)于pycharm2023.1配置python解釋器時(shí)找不到conda環(huán)境的解決辦法,需要的朋友可以參考下2023-12-12

