Python 2.7 需要将\u mobile的no\u转换为整数格式 需要将“no_of_mobile”变量转换为整数格式,并使用循环打印

Python 2.7 需要将\u mobile的no\u转换为整数格式 需要将“no_of_mobile”变量转换为整数格式,并使用循环打印,python-2.7,Python 2.7,在这里, 如果您还需要品牌名称,那么应该修改解析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"

在这里, 如果您还需要品牌名称,那么应该修改解析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)

你能回答你的问题吗?这真的不清楚。需要将所有的no_of_mobile转换为整数格式,并使用loop只打印几个top integer。你能用这些信息回答你的问题吗?评论是给我们的:)@nishantkumar好吧。。通过单击答案中的勾号接受答案
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)