提问人:Artem Gorbunov 提问时间:11/12/2023 更新时间:11/12/2023 访问量:29
如何使用 find_all beautilullsoup 从 html 代码中解析必要的对象
How to parse a necessary object from html code using find_all beautilullsoup
问:
我使用 python 编写解析代码,遇到了麻烦:
我不知道如何使用页面(https://buff.163.com/market/csgo#tab=selling&page_num=1)获取必要的对象
F.e. 我想获得名称为“StatTrak™ MAG-7 |Cobalt Core (Well-Worn)': 我看到很多带有标签的 obeject,其中第一个是我需要的
我怎样才能得到这个物体和他的价格?
我使用这段代码来实现这个目的:
url = 'https://buff.163.com/market/csgo#tab=selling&page_num=1'
main_url='https://buff.163.com/'
request = requests.get(url)
req = request.text
soup = BeautifulSoup(req, 'lxml')
li = soup.find_all('li')
li
但我不明白我怎样才能找到正确的标签
答:
0赞
Andrej Kesely
11/12/2023
#1
您在浏览器中看到的数据是使用 JavaScript 从外部源加载的。您可以使用以下示例如何获取项目数据:
import requests
url = "https://buff.163.com/api/market/goods"
params = {"game": "csgo", "page_num": "1", "use_suggestion": "0"}
data = requests.get(url, params=params, cookies={"Locale-Supported": "en"}).json()
for i in data["data"]["items"]:
print(f'{i["name"]:<50} {i["quick_price"]}')
指纹:
Nova | Windblown (Factory New) 2.12
★ Gut Knife | Bright Water (Minimal Wear) 664.5
MAC-10 | Red Filigree (Factory New) 563.5
★ Karambit | Stained (Battle-Scarred) 3285.5
Dual Berettas | Tread (Minimal Wear) 3.65
Souvenir USP-S | Desert Tactical (Well-Worn) 3.73
SSG 08 | Azure Glyph (Factory New) 4.19
Souvenir CZ75-Auto | Silver (Factory New) 3.96
StatTrak™ Tec-9 | Brother (Well-Worn) 3.67
Souvenir PP-Bizon | Carbon Fiber (Factory New) 3.83
Souvenir PP-Bizon | Facility Sketch (Minimal Wear) 3.9
XM1014 | Teclu Burner (Battle-Scarred) 4.06
SSG 08 | Necropos (Minimal Wear) 3.98
MP9 | Goo (Minimal Wear) 3.78
StatTrak™ P2000 | Wicked Sick (Field-Tested) 31.41
AWP | POP AWP (Minimal Wear) 24.7
StatTrak™ AWP | Exoskeleton (Battle-Scarred) 27.02
XM1014 | Entombed (Minimal Wear) 25.13
AK-47 | Baroque Purple (Minimal Wear) 29.89
StatTrak™ Nova | Wild Six (Minimal Wear) 7.69
评论
0赞
Artem Gorbunov
11/12/2023
多谢!它有效 但是您能解释一下,您如何理解使用“buff.163.com/api/market/goods”链接而不是“buff.163.com/market/csgo#tab=selling&page_num=1”提取数据的正确方法吗?如果我访问该网站并点击链接“市场”,我会看到该网站,网址为“buff.163.com/market/csgotab=selling&page_num=1”,而不是您的链接感谢您抽出时间!
0赞
Andrej Kesely
11/12/2023
@ArtemGorbunov 当您在浏览器中打开 Web 开发人员工具并切换到“网络”选项卡时,重新加载页面 - 您将在那里看到 API URL。
评论