【问题标题】:Python How to sort values when they are all in body?Python当它们都在体内时如何对值进行排序?
【发布时间】:2020-10-31 10:04:55
【问题描述】:

import requests
import time
from bs4 import BeautifulSoup


start_time = time.time()
url = 'https://secure.runescape.com/m=hiscore_oldschool/index_lite.ws?player=zezima'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'}

resp = requests.get(url, headers=headers)
print("Server: " + resp.headers['server'])
print(resp.text)
print("Job done in" + " %f seconds" % (time.time() - start_time))

我正在尝试学习如何对不同的值(它们的统​​计数据)进行排序并执行例如 总等级:1465
统计数据总是以相同的顺序排列,如果没有找到,则为 -1。 我希望它只输出第二个值的总水平。在这种情况下,1465。 理想的输出应该是这样的:

总等级:1465

示例响应:

690953,1465,27957906
711820,76,1343681
658073,76,1342072
1033645,75,1271864
1091165,75,1252551

【问题讨论】:

    标签: python extract


    【解决方案1】:

    这是一个简单的换行符分割,然后用逗号分割第一行的例子。

    lines = resp.text.split('\n')
    first_line = lines[0].split(',')
    print('Total level: ' + first_line[1] )
    

    如果不总是有结果,您可以添加一些错误检查。

    【讨论】:

      【解决方案2】:
       import requests 
       import bs4 
       import time
      
       url  = 'https://books.toscrape.com/catalogue/page-1.html' # this link will be usefull 
       its made for webscraping , and all price is fake 
       # you can practies on this website ...
       start  = time.time()
      
       req = requests.get(url)
       soup = bs4.BeautifulSoup(req.text,'lxml') # using BeautifulSoup for sorting html 
       #script
      
       end = time.time()
       print("Job done in" + " %f seconds" % (end - start))
      
      #if you want something in the website if it was class use (.) before the stuf  if we 
      want product price of first page of books
      product_price = soup.select('.price_color')
      
      for i in product_price:
      print('price = ' +i.getText())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-14
        • 2019-09-28
        • 1970-01-01
        • 2021-02-07
        • 2016-02-26
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        相关资源
        最近更新 更多