url = "https://buff.163.com/api/market/goods/sell_order?game=csgo&goods_id=%goods_id%&page_num=1" def get_buff_low_price (buff_cookie,goods_id,float_rank): print(goods_id) # 定义请求头,包含 API Key headers = { "User-Agent": "Mozilla/5.0", "Accept-Language": "en-US,en;q=0.5", } headers['Cookie'] = buff_cookie base_url="https://buff.163.com/api/market/goods/sell_order?game=csgo&goods_id=%goods_id%&page_num=1" base_url= base_url.replace('%goods_id%',str(goods_id)) res_url='' if(float_rank=='luemo_1'): res_url = '&sort_by=default&mode=&allow_tradable_cooldown=1&min_paintwear=0.07&max_paintwear=0.08' elif(float_rank=='luemo_2'): res_url = '&sort_by=default&mode=&allow_tradable_cooldown=1&min_paintwear=0.08&max_paintwear=0.09' else: res_url = '' url =base_url+res_url #print(url) # 发送 GET 请求,获取 API 返回结果 while True: # 一直循环,知道访问站点成功 try: # 以下except都是用来捕获当requests请求出现异常时, # 通过捕获然后等待网络情况的变化,以此来保护程序的不间断运行 response = requests.get(url, headers=headers, timeout=20) break except requests.exceptions.ConnectionError: print('ConnectionError -- please wait 3 seconds') time.sleep(3) except requests.exceptions.ChunkedEncodingError: print('ChunkedEncodingError -- please wait 3 seconds') time.sleep(3) except: print('Unfortunitely -- An Unknow Error Happened, Please wait 3 seconds') time.sleep(3) # 解析返回结果 json_data = json.loads(response.text) #print(json_data) buff_price = json_data['data']['items'][0]['price'] return buff_price print(buff_price) print(json_data)
时间: 2024-04-07 10:31:41 浏览: 178
感谢您提供完整的代码片段。根据您的代码,问题出在了最后两行。您在执行 `return buff_price` 之后就直接打印了 `buff_price` 和 `json_data`,但实际上这两行代码永远不会被执行,因为在 `return` 之后函数就会立即退出了。
请将 `print(buff_price)` 和 `print(json_data)` 这两行代码放到 `return` 语句之前,这样就可以在返回 `buff_price` 之前打印出这两个变量的值了。
相关问题
"http://170.106.117.230/?url=steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M4339827378402747041A30648261933D3039437502918428834"
Based on the URL you provided, it looks like you are trying to preview an in-game item from the game Counter-Strike: Global Offensive (CSGO). The item ID is M4339827378402747041A30648261933D3039437502918428834. However, the IP address in the URL does not seem to be a valid Steam server IP address. Please make sure you have entered the correct URL or IP address.
import requests from bs4 import BeautifulSoup 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') # 找到特定的标签或者CSS选择器 data = soup.find('div', {'class': 'floatvalue'}).text # 打印提取的数据 print(soup )怎么提取floatbalue
可以使用 BeautifulSoup 的 `find` 或 `find_all` 方法来查找特定的标签或 CSS 选择器,并使用 `text` 属性来获取标签中的文本内容。在这个例子中,可以使用以下代码来提取 `floatvalue`:
```
data = soup.find('div', {'class': 'floatvalue'}).text
print(data)
```
这会在 HTML 中查找 `class` 属性为 `floatvalue` 的 `div` 标签,并将其文本内容提取出来。
阅读全文