2016-09-23 67 views
-2

需要轉換「no_of_mobile」變量的使用環路

import requests 
import re 
from bs4 import BeautifulSoup 

request = requests.get("https://priceraja.com/mobile/brands/").text 
soup = BeautifulSoup(request,"html.parser") 
mspec = soup.find('ul', attrs = {"class","top-brands"}) 
list=[] 
no_of_mobile=[] 
for link in mspec.find_all('li'): 
    li=link.getText('li') 
    lit =str(li).split("\n") 
    litt=lit 
    no_of_mobile= litt[1].replace('(', '').replace(')', '') 
    print no_of_mobile 
+1

你可以[編輯]你的問題嗎?它真的不清楚。 –

+0

需要將所有no_of_mobile轉換爲整數格式,並使用循環僅打印幾個最大整數 –

+0

您能否使用該信息編輯您的問題?評論是爲我們:) –

回答

0

這, 如果你想要的品牌名稱也,你應該修改你在哪裏代碼爲整型和打印解析html文件並相應地打印。

import requests 
import re 
from bs4 import BeautifulSoup 

request = requests.get("https://priceraja.com/mobile/brands/").text 
soup = BeautifulSoup(request,"html.parser") 
mspec = soup.find('ul', attrs = {"class","top-brands"}) 
list=[] 
no_of_mob=[] ## Note the change in list name 
for link in mspec.find_all('li'): 
    li=link.getText('li') 
    lit =str(li).split("\n") 
    litt=lit 
    no_of_mobile= litt[1].replace('(', '').replace(')', '') 
    no_of_mob.append(int(no_of_mobile)) ## appending the values to the list, here type of the object is string. 
no_of_mob=sorted(set(no_of_mob), key=lambda x:int(x),reverse=True) ## sorting the converted integer values 
print no_of_mob[:5] ## printing top 5 (Highest no of mobiles) 
+0

謝謝@siva –

+0

@nishantkumar請查看你的查詢是否已解決 –

+0

@nishantkumar好吧然後..接受答案通過點擊答案中的勾號 –