import requests from bs4 import BeautifulSoup import json url ="http://170.106.117.230/?url=steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M4339827378402747041A30648261933D3039437502918428834" headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'} response = requests.get(url, headers=headers) print('状态码:', response.status_code) print('响应体:', response.text) soup = BeautifulSoup(response.text, 'html.parser') print(soup.prettify()) # 找到特定的标签或者CSS选择器 item_info = '{"iteminfo":{"origin":8,"quality":4,"rarity":5,"a":"30648261933","d":"3039437502918428834","paintseed":582,"defindex":32,"paintindex":1224,"stickers":[],"floatid":"30648261933","low_rank":125,"high_rank":134,"floatvalue":0.34608370065689087,"m":"4339827378402747041","s":"0","imageurl":"http://media.steampowered.com/apps/730/icons/econ/default_generated/weapon_hkp2000_cu_p2000_decline_light_large.7f57145674a5e41b3b8e7fe70be4ffbb57ec6f84.png","min":0,"max":1,"weapon_type":"P2000","item_name":"Wicked Sick","rarity_name":"Classified","quality_name":"Unique","origin_name":"Found in Crate","wear_name":"Field-Tested","full_item_name":"P2000 | Wicked Sick (Field-Tested)"}' # 将JSON字符串解析为字典 item_dict = json.loads(item_info) # 提取floatvalue的数值 floatvalue = item_dict['iteminfo']['floatvalue'] print(floatvalue) # 打印提取的数据 floatvalue_tag = soup.find('iteminfo') if floatvalue_tag: data = floatvalue_tag.text print(data) else: print('未找到 floatvalue 标签')
时间: 2023-08-11 17:05:35 浏览: 112
python爬虫练习:爬图片
这段代码是用 Python 爬取 Steam 市场上某个商品的信息,并提取其中的 floatvalue 值。首先,使用 requests 库发送 GET 请求获取网页内容,然后用 BeautifulSoup 库解析网页内容,找到特定的标签或者 CSS 选择器,提取出需要的信息。接着,将 JSON 字符串解析为字典,再从字典中提取出 floatvalue 值,并打印出来。最后,使用 BeautifulSoup 找到包含 floatvalue 的标签,如果找到则打印出其中的文本,否则提示未找到。
阅读全文