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

首頁 > 編程 > Python > 正文

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

2020-01-04 14:54: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.striplstrip,rstrip的用法:

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

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

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

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

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

yes no

比較簡單吧,lstriprstrip原理是一樣的。

注意:當沒有傳入參數時,是默認去除首尾空格和換行符的。

theString = 'saaaay yes no yaaaass'print theString.strip('say')print theString.strip('say ') #say后面有空格print theString.lstrip('say')print theString.rstrip('say')

運行結果:

yes no
es no
yes no yaaaass
saaaay yes no

希望本文所述對大家Python程序設計有所幫助。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闽侯县| 汤阴县| 明水县| 遂昌县| 措美县| 芜湖市| 喀喇沁旗| 兴国县| 堆龙德庆县| 鹤庆县| 视频| 准格尔旗| 吉林市| 沙河市| 伊吾县| 永清县| 仪陇县| 栾川县| 伊宁市| 孟津县| 广灵县| 航空| 洞头县| 长沙县| 富民县| 苗栗县| 阜城县| 镇坪县| 乌什县| 华坪县| 桃源县| 灵宝市| 翁牛特旗| 香格里拉县| 西林县| 嵊泗县| 北碚区| 宣武区| 杨浦区| 五台县| 平昌县|