【发布时间】:2021-05-26 14:22:36
【问题描述】:
我有一个代码要在这个站点中搜索 --> https://osu.ppy.sh/beatmapsets?m=0 只映射我想要的难度,但我无法正确循环
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
# Set link and path
driver = webdriver.Chrome(executable_path=r"C:\Users\Gabri\anaconda3\chromedriver.exe")
driver.get("https://osu.ppy.sh/beatmapsets?m=0")
wait = WebDriverWait(driver, 20)
# Variables, lists and accountants
lista = {}
links, difficulty, maps2, final = [], [], [], []
line, column, = 1, 1
link_test = ''
n = int(input('insert how many maps do you want: '))
c = 1
# Open link in Chrome and search map by map
while True:
if c > n:
break
sleep(1)
wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, f".beatmapsets__items-row:nth-of-type(1)>.beatmapsets__item:nth-of-type(1)")))
games = driver.find_element_by_css_selector(
f".beatmapsets__items-row:nth-of-type({line}) .beatmapsets__item:nth-of-type({column}) .beatmapset-panel__info-row--extra")
actions = ActionChains(driver)
actions.move_to_element(games).perform()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".beatmaps-popup__group")))
scores = driver.find_elements_by_css_selector(
".beatmaps-popup__group .beatmaps-popup-item__col.beatmaps-popup-item__col--difficulty")
# This part i can't makes automatic, for example, if i wanted to show 6 maps i would have to add 2 more if's
# Changing the variable (line) and (column) accordingly
# I liked to have a loop with 'while' or 'for ... in' but i don't know how make it
# I tried to do a question before start the code like 'how many maps do you want?' and this number would be the times that code would execute
# But no work it =(
if c % 2 != 0:
column = 2
if c % 2 == 0:
line += 1
else:
line += 1
column = 1
# Convert string to float (difficulty numbers)
for score in scores:
a = score.text
b = a.replace(',', '.')
difficulty.append(float(b))
# Save in list 'links' each link corresponding of map that is printing
games.click()
sleep(3)
link_test = driver.current_url
links.append(link_test)
link_test = ''
driver.back()
# Dict with map, link and difficulty
lista = {
'map': f"{c}",
'link': f"{links}",
'difficulty': f"{difficulty}"}
c += 1
# Print each map in dict 'lista'
print(f"Map: {lista['map']}\nLink: {links}\nDifficulty: {lista['difficulty']}\n")
# This part is my filter, if map have difficulty 6.00 or more, it's add to list 'final' for download
for b in difficulty:
if b >= 6.00:
# This slice, the link had printing error 'TypeError: unhashable type: 'list'', i found this way to solve it
# I know that is not the best way to solve this error, but at least i tried =,)
xam = str(links[0])
xam1 = xam.replace("'", '')
xam2 = xam1.replace("[", '')
xam3 = xam2.replace("]", '')
final.append(xam3)
# Clean all lists for no have duplicate items in dict 'lista' when next map is selected
difficulty.clear()
lista.clear()
links.clear()
# Print how many maps with difficulty 6.00 has been found
print(f'There are {len(sorted(set(final)))} maps to download')
# This question is for future download, im still coding this part, so u can ignore this =3
pergunta = input('Do you want to download them? \n[ Y ]\n[ N ]\n>>> ').lower().strip()
# Clean duplicate links and show all links already filtered
if pergunta == 'y':
for x in final:
maps2.append(x)
print(sorted(set(maps2)))
在“如果”部分,我需要帮助以使其自动化,就像我所做的那样,对许多“如果”没有用处。使用带有'v += n'的变量可能?身份证;-;
PS-如果您发现任何逻辑错误或优化我的代码的方法,我将很乐意学习并修复它
【问题讨论】:
-
我已经看到这个帖子至少 3 次了,最近 2 次尝试中什么对你不起作用?
-
@cruisepandey 最近 2 次我没有尝试对这部分做任何事情,因为我将注意力集中在解决其他问题上。我之前一直在等待有人帮助我,但今天我试图解决这个问题问题,此时我尝试用 += 制作一个简单的自会计变量,如果我向前迈出一步,我将编辑代码来解释我做了什么
-
随着
maps_quantity数量的增加,line和column是否有任何模式?从上面的代码 sn-p 看来是随机的 -
@JD2775 是的,就像一个坐标,第1行第1列是1°图,第1行第2列是2°图,第2行第1列是3°图...我按照布局页面的,有两列和几行
-
@JD2775 看这个例子 --> imgur.com/a/NtbBxXL
标签: python selenium selenium-webdriver