【问题标题】:Website scraping - How to get parameters from div class网站抓取 - 如何从 div 类中获取参数
【发布时间】:2022-01-03 12:05:07
【问题描述】:

我想使用 python 的 BeautifulSoup 从这部分 html 代码中提取有关年份、公里数和颜色的信息。有人可以帮帮我吗?

                    <h3 class="topNavTitle" id="techParams">Tech parms</h3>
                    <div class="techParamsRow general">
                        <div class="col col12M pr20">
                            <table class="transparentTable">
                                <tr>
                                    <th>Year</th><td>2014</td>
                                </tr>
                                <tr>
                                    <th>Kms</th><td>103 472 km</td>
                                </tr>
                                <tr>
                                    <th>Color</th><td>white</td>
                                </tr>
                            </table>
                        </div>

我试过了:

res = requests.get(website)
soup = BeautifulSoup(res.content, "html.parser")
results = soup.find('div', {'class': 'techParamsRow general'})
print(results)

但它什么也没找到。 谢谢!

【问题讨论】:

  • 未找到任何东西是什么意思 - 您的选择不是特定于获取表格,但它会找到您的 div。总是先看看你的汤——这就是真相。 print(soup) -> 你在汤里找到 html 了吗?使用信息更新您的问题,并提供您的预期结果。谢谢

标签: python html web-scraping beautifulsoup


【解决方案1】:

如果我理解正确,你可以使用 css 选择器做你想做的事:

data = soup.select('table.transparentTable tr')
for d in data:
  row = d.select('th,td')
  print(row[0].text,":",row[1].text)

输出:

Year : 2014
Kms : 103 472 km
Color : white

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 2014-01-21
    • 2019-08-27
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多