Как выводить все href из всех тэгов?

from telebot import TeleBot
from bs4 import BeautifulSoup
import requests





token = 'token'

bot = TeleBot(token)





@bot.message_handler(content_types=['text'])


def repeat_all_messages(message):

    print(message.text)
    search = message.text

    shmak = search.replace(' ', "+")

    URL = 'https://store.steampowered.com/search/?term='+ shmak


    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19041'
    }

    responce = requests.get(URL,headers=headers)
    soup = BeautifulSoup(responce.text, 'lxml')
    games = soup.find_all('a', class_="search_result_row ds_collapse_flag")


    for n, i in enumerate(games, start=1):
        div = i.find('div', class_='col search_name ellipsis').find('span', class_='title')
        price = i.find('div', {"class": ["col search_price discounted responsive_secondrow", "col search_price responsive_secondrow"]})

        links = soup.find('a', class_="search_result_row ds_collapse_flag").get('href')
        x = str(n) + ':' + div.text + ' ' + str(price.text)


        bot.send_message(message.chat.id,f'{x}\n\n{links}')
        #bot.send_message(message.chat.id,links)



bot.infinity_polling()
  • Вопрос задан
  • 75 просмотров
Решения вопроса 1
hottabxp
@hottabxp Куратор тега Python
Сначала мы жили бедно, а потом нас обокрали..
F12 -> видим, что запрос нужно отправлять по адресу "https://store.steampowered.com/search/results?term=", например - "https://store.steampowered.com/search/results?term..."

И ещё можно вынести парсинг в отдельную функцию, чтобы было проще отлаживать:
from bs4 import BeautifulSoup
import requests


def search_game(title):
	URL = 'https://store.steampowered.com/search/results'

	games_list = []

	response = requests.get(URL,params={'term':title},headers={'user-agent':'hacked by hottabXP'})
	soup = BeautifulSoup(response.text,'lxml')

	games = soup.find('div', id='search_resultsRows').find_all('a',class_='search_result_row ds_collapse_flag')
	for game in games:
		title = game.find('span',class_='title').text.strip()
		try: # На случай, если цена со скидкой
			price = game.find('div',class_='col search_price responsive_secondrow').text.strip()
		except:
			price = game.find('strike').text
		games_list.append({'title':title,'price':price})

	return games_list

print(search_game("GTA 5"))

[{'title': 'Grand Theft Auto V', 'price': ''}, {'title': 'Grand Theft Auto V: Premium Edition', 'price': '629₴'}, {'title': 'Grand Theft Auto V: Premium Edition & Great White Shark Card Bundle', 'price': '790₴'}, {'title': 'Grand Theft Auto V: Premium Edition & Megalodon Shark Card Bundle', 'price': '1 228₴'}, {'title': 'GTA Online: Shark Cash Cards', 'price': ''}, {'title': 'Grand Theft Auto V - Criminal Enterprise Starter Pack', 'price': '209₴'}, {'title': 'Grand Theft Auto V: Premium Edition & Whale Shark Card Bundle', 'price': '916₴'}, {'title': 'liteCam Game 5:  120 FPS Game Capture + Streamer', 'price': ''}, {'title': 'Assetto Corsa - Dream Pack 1', 'price': '129₴'}, {'title': 'Warhammer 40,000: Dawn of War III', 'price': '599₴'}, {'title': 'Memory Card Monsters - Expanded Content 5', 'price': '21₴'}, {'title': 'Fantasy Memory Card Game - Expansion Pack 5', 'price': '21₴'}, {'title': 'APB Reloaded', 'price': 'Free to Play'}, {'title': 'Shop Tycoon: Prepare your wallet', 'price': '119₴'}, {'title': '3dSunshine', 'price': 'Free'}, {'title': 'Druid', 'price': '20₴'}, {'title': 'Lazaretto', 'price': '129₴'}, {'title': "Rock 'N' Roll Defense", 'price': '21₴'}, {'title': '小黑盒加速器', 'price': 'Free To Play'}, {'title': 'Drop Hunt - Adventure Puzzle', 'price': '20₴'}, {'title': 'Carmageddon Max Pack', 'price': '169₴'}, {'title': 'Lamp Head', 'price': '27₴'}, {'title': 'Need for Drive - Open World Multiplayer Racing', 'price': '21₴'}, {'title': 'Crash Drive 3', 'price': '279₴'}, {'title': 'Retro City Rampage™ DX', 'price': '229₴'}, {'title': 'VCB: Why City 4k', 'price': '67₴'}, {'title': 'CS2D', 'price': 'Free To Play'}, {'title': 'ConflictCraft', 'price': '21₴'}, {'title': 'State of Anarchy: Master of Mayhem', 'price': '20₴'}, {'title': 'Geneshift', 'price': '229₴'}, {'title': '汉武大帝传', 'price': '149₴'}, {'title': 'Fix Me Up Doc! – Dark Humor', 'price': '21₴'}, {'title': 'Extreme Racing on Highway', 'price': '179₴'}, {'title': 'The Last Hope: Trump vs Mafia - North Korea', 'price': '21₴'}, {'title': 'Control Craft 3', 'price': '21₴'}, {'title': 'Zombie Killer Drift - Racing Survival', 'price': '21₴'}, {'title': 'Bugs Must Die / 异星特勤队', 'price': '199₴'}, {'title': 'Control Craft 2', 'price': '21₴'}, {'title': 'Hero Hunters - 杀手 3D 2K19', 'price': '119₴'}, {'title': 'USA Truck Simulator', 'price': '27₴'}, {'title': 'MechDefender - Tower Defense', 'price': '21₴'}, {'title': 'CYBER.one: Racing For Souls', 'price': ''}, {'title': 'VCB: Why City (Beta Version)', 'price': ''}, {'title': 'Mountain Taxi Driver', 'price': '21₴'}, {'title': 'The Kickstarter Avoidance Album', 'price': '21₴'}, {'title': 'Dino Zoo Transport Simulator', 'price': '27₴'}, {'title': 'Snow Clearing Driving Simulator', 'price': '169₴'}, {'title': 'Space Hero Line', 'price': '21₴'}, {'title': 'EDEN STAR', 'price': '279₴'}, {'title': 'liteCam Game: 100 FPS Game Capture', 'price': ''}]
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
01 мая 2024, в 02:11
5000 руб./за проект
01 мая 2024, в 00:29
2000 руб./за проект
01 мая 2024, в 00:20
15000 руб./за проект