国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 额尔古纳市| 辽阳县| 津市市| 通化县| 洪洞县| 福建省| 祁连县| 泗阳县| 扶风县| 宜君县| 揭西县| 巴南区| 富民县| 库车县| 临漳县| 浮山县| 犍为县| 波密县| 莲花县| 柞水县| 肥城市| 新巴尔虎右旗| 广元市| 永川市| 泸西县| 晋中市| 鹰潭市| 连州市| 六盘水市| 德格县| 邛崃市| 尤溪县| 林甸县| 九龙坡区| 西峡县| 阿巴嘎旗| 扶余县| 东辽县| 宜川县| 江源县| 怀来县|