gpt4 book ai didi

python - 如何按降序排列我的链接(我有链接的值,(num_to_words(v)))

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:35 26 4
gpt4 key购买 nike

我正在制作网络爬虫,现在我需要排序算法,它可以按降序对我的链接进行排序,以查看哪个链接出现在该网页中的次数最多。这是我用 python 编写的代码:

import requests
from bs4 import BeautifulSoup
from collections import defaultdict

all_links = defaultdict(int)

def webpages():

url = 'http://www.hm.com/lv/department/MEN'
source_code = requests.get(url)
text = source_code.text
soup = BeautifulSoup(text)
for link in soup.findAll ('a', {'class':' ', 'rel':'nofollow'}):
href = link.get('href')
print(href)
get_single_item_data(href)
return all_links

def get_single_item_data(item_url):
source_code = requests.get(item_url)
text = source_code.text
soup = BeautifulSoup(text)
for link in soup.findAll('a'):
href = link.get('href')
if href and href.startswith('http://www.'):
if href:
all_links[href] += 1
print(href)


webpages()

units = ["", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine "]
teens = ["", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", 'sixteen', "seventeen", "eighteen", "nineteen"]
tens = ["", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"]
thousands = ["", "thousand", "million", "billion", "trillion",
"quadrillion", "quintillion", "sextillion", "septillion", "octillion",
"nonillion", "decillion", "undecillion", "duodecillion", "tredecillion",
"quattuordecillion", "sexdecillion", "septendecillion", "octodecillion",
"novemdecillion", "vigintillion "]


def num_to_words(n):
words = []
if n == 0:
words.append("zero")
else:
num_str = "{}".format(n)
groups = (len(num_str) + 2) // 3
num_str = num_str.zfill(groups * 3)
for i in range(0, groups * 3, 3):
h = int(num_str[i])
t = int(num_str[i + 1])
u = int(num_str[i + 2])
print()
print(units[i])
g = groups - (i // 3 + 1)
if h >= 1:
words.append(units[h])
words.append("hundred")
if int(num_str) % 100: # if number modulo 100 has remainder add "and" i.e one hundred and ten
words.append("and")
if t > 1:
words.append(tens[t])
if u >= 1:
words.append(units[u])
elif t == 1:
if u >= 1:
words.append(teens[u])
else:
words.append(tens[t])
else:
if u >= 1:
words.append(units[u])

if g >= 1 and (h + t + u) > 0:
words.append(thousands[g])
return " ".join(words)

for k, v in webpages().items():
print(k, num_to_words(v))

最佳答案

如果它们存储在数组中,您可以对数组进行排序。
例如:

# Array 
a = [6, 2, 9, 3]
# sort the array
a.sort()

也许此链接也有帮助:Link about sorting

关于python - 如何按降序排列我的链接(我有链接的值,(num_to_words(v))),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28517047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com