import re
import requests
from bs4 import BeautifulSoup
#
def get_market_search_html(target):
search_url = r'https://steamcommunity.com/market/search?q='
headers = {
'Accept': 'text/html,application/xhtml+xml,'
'application/xml;q=0.9,image/avif,'
'image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'ru,en;q=0.9',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/98.0.4758.119 YaBrowser/22.3.0.2430 Yowser/2.5 Safari/537.36'
}
resp = requests.get(search_url + target.replace(' ','+'), headers=headers)
if resp.status_code == 200:
return resp.text
#
def get_match_list(soup:BeautifulSoup):
out = []
match0 = soup.find('div',id="searchResultsRows")
if match0:
match1 = match0.find_all('a')
if match1:
for m in match1:
item_name = m.find('span', class_='market_listing_item_name')
item_price = m.find(lambda tag: tag.name == 'span' and tag.get('class') == ['normal_price'])
out.append((item_name.text if item_name else None, item_price.text if item_price else None))
return out
#
def main():
print('что ищем?')
while True:
target = input('>>>')
if target == '0':# для выхода из цикла 0
break
html = get_market_search_html(target)
if html:
soup = BeautifulSoup(html, 'html.parser')
res = get_match_list(soup)
if res:
for i in res:
print(i)
else:
print('math_list not found')
else:
print('html not received')
#
if __name__ == '__main__':
main()
>>>
что ищем?
>>>awp фобос
('AWP | Фобос (Поношенное)', '$3.05 USD')
('AWP | Фобос (Немного поношенное)', '$1.23 USD')
('StatTrak™ AWP | Фобос (Поношенное)', '$6.11 USD')
('AWP | Фобос (После полевых испытаний)', '$1.17 USD')
('AWP | Фобос (Прямо с завода)', '$2.41 USD')
('StatTrak™ AWP | Фобос (Немного поношенное)', '$4.32 USD')
('StatTrak™ AWP | Фобос (Прямо с завода)', '$6.46 USD')
('StatTrak™ AWP | Фобос (После полевых испытаний)', '$3.83 USD')