国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python爬蟲實現抓取京東店鋪信息及下載圖片功能示例

2020-02-15 22:41:25
字體:
來源:轉載
供稿:網友

本文實例講述了Python爬蟲實現抓取京東店鋪信息及下載圖片功能。分享給大家供大家參考,具體如下:

這個是抓取信息的

from bs4 import BeautifulSoupimport requestsurl = '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')                   #.text將解析到的網頁可讀storenames = soup.select('#J_ItemList > div > div > p.productTitle > a')    #選擇出商店的信息prices = soup.select('#J_ItemList > div > div > p.productPrice > em')     #選擇出價格的信息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()方法篩選出標簽中的文本信息,由于篩選結果有換行符/n所以用strip()將換行符去掉  price = price.get_text()  sale = sale.get_text()  print('商店名:%-40s價格:%-40s銷售額:%s'%(storename,price,sale))   #使打印出來的信息規范  print('----------------------------------------------------------------------------------------------')

這個是下載圖片的

from bs4 import BeautifulSoupimport requestsimport urllib.requesturl = '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 = 1for i in imgs:  if(i.get('src')==None):    break  img = 'http:'+i.get('src') #這里廢了好長的時間,原來網站必須要有http:的  #print(img)  urllib.request.urlretrieve(img,'%s.jpg'%a, None,)  a = a+1

ps:

1.選擇信息的時候用css

2.用get_text()方法篩選出標簽中的文本信息

3.striplstriprstrip的用法:

Python中的strip用于去除字符串的首尾字符;同理,lstrip用于去除左邊的字符;rstrip用于去除右邊的字符。

這三個函數都可傳入一個參數,指定要去除的首尾字符。

需要注意的是,傳入的是一個字符數組,編譯器去除兩端所有相應的字符,直到沒有匹配的字符,比如:

theString = 'saaaay yes no yaaaass'print theString.strip('say')

theString依次被去除首尾在['s','a','y']數組內的字符,直到字符在不數組內。所以,輸出的結果為:

yes no

比較簡單吧,lstriprstrip

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘孜县| 岚皋县| 虞城县| 彩票| 绿春县| 武城县| 长岛县| 高碑店市| 马边| 浠水县| 临夏县| 安图县| 金沙县| 苍溪县| 中方县| 周宁县| 顺昌县| 肥城市| 高青县| 本溪市| 武强县| 淳化县| 防城港市| 江门市| 宣恩县| 汤阴县| 德清县| 景洪市| 息烽县| 宝坻区| 江西省| 安泽县| 邵阳市| 清远市| 南岸区| 彝良县| 英超| 定兴县| 闽侯县| 大渡口区| 元氏县|