英文:
How to find data with specific tags in Python using requests module?
问题
这是我需要从中获取内容的当前HTML部分。
<table class="table table-hover sortable-theme-minimal table-heatmap" data-sortable="">
<thead>
<tr>
<th>
</th>
<th>
Price
</th>
<th>
</th>
<th>
</th>
<th data-heatmap="1" data-heatmap-limit="5" style="text-align: center;cursor:pointer;">
Day
</th>
<th data-heatmap="1" data-heatmap-limit="20" style="text-align: center;cursor:pointer">
Month
</th>
<th data-heatmap="1" data-heatmap-limit="100" style="text-align: center;cursor:pointer">
Year
</th>
<th class="hidden-xs" style="text-align: center;">
Date
</th>
</tr>
</thead>
<tr data-decimals="3" data-subscribe="CL1:COM" data-symbol="CL1:COM">
<td>
<a href="/commodity/crude-oil">
Crude Oil
</a>
</td>
<td id="p">
82.86
我需要获取那个82.86的数字,但是我似乎无法指定到tr data-decimals="3" data-subscribe="CL1:COM" data-symbol="CL1:COM"
这一行。
以下是我当前的代码:
URL = "https://tradingeconomics.com/commodity/crude-oil"
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find("table", class_="table table-hover sortable-theme-minimal table-heatmap")
results1 = results.find("tr", text="data-symbol=\"CL1:COM\"")
print(results.prettify())
有没有办法指定"data-symbol"类并从其下的id="p"的td中获取数据?
英文:
This is the current HTML part that I need to get something from.
<table class="table table-hover sortable-theme-minimal table-heatmap" data-sortable="">
<thead>
<tr>
<th>
</th>
<th>
Price
</th>
<th>
</th>
<th>
</th>
<th data-heatmap="1" data-heatmap-limit="5" style="text-align: center;cursor:pointer;">
Day
</th>
<th data-heatmap="1" data-heatmap-limit="20" style="text-align: center;cursor:pointer">
Month
</th>
<th data-heatmap="1" data-heatmap-limit="100" style="text-align: center;cursor:pointer">
Year
</th>
<th class="hidden-xs" style="text-align: center;">
Date
</th>
</tr>
</thead>
<tr data-decimals="3" data-subscribe="CL1:COM" data-symbol="CL1:COM">
<td>
<a href="/commodity/crude-oil">
Crude Oil
</a>
</td>
<td id="p">
82.86
I need to get that 82.86 number, but I can't seem to specify to the "tr data-decimals="3" data-subscribe="CL1:COM" data-symbol="CL1:COM"" line.
Here is my current code:
URL = "https://tradingeconomics.com/commodity/crude-oil"
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find("table", class_="table table-hover sortable-theme-minimal table-heatmap")
results1 = results.find("tr", text="data-symbol=\"CL1:COM\"")
print(results.prettify())
Is there a way to specify the "data-symbol" class and get the data from the id="p" td under it?
答案1
得分: 1
你可以使用Beautiful Soup,尝试以下代码:
from bs4 import BeautifulSoup
import requests
URL = "https://tradingeconomics.com/commodity/crude-oil"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
target_tr = soup.find("tr", attrs={"data-symbol": "CL1:COM"})
price_td = target_tr.find("td", id="p")
price = price_td.get_text(strip=True)
print("价格:", price)
另外,Selenium也可能会有用。祝好运!
英文:
You can use Beautiful Soup, try this:
from bs4 import BeautifulSoup
import requests
URL = "https://tradingeconomics.com/commodity/crude-oil"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
target_tr = soup.find("tr", attrs={"data-symbol": "CL1:COM"})
price_td = target_tr.find("td", id="p")
price = price_td.get_text(strip=True)
print("Price:", price)
Also Selenium could prove useful..gl
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论