【问题标题】:How to scrape the new format for Product information on Amazon.com using BeautifulSoup?如何使用 BeautifulSoup 在 Amazon.com 上抓取新格式的产品信息?
【发布时间】:2017-10-18 15:52:40
【问题描述】:

在这个post 中,alecxe 给出了如何抓取 Amazon.com 产品信息/产品详细信息表的解决方案。但是,该描述表的格式与亚马逊上列出的许多较新项目不同。

您可以看到here 的旧格式与新格式here 不同。

我尝试了什么:在他使用的 alecxe 给出的代码中

for li in soup.select('table#productDetailsTable div.content ul li'):

我尝试将其更改为(并删除之后的所有内容):

for tr in soup.select('table#productDetails_detailBullets_sections1 tbody tr'):
    print text.tr
    print(repr(tr))

看看我是否能够从产品信息表中至少提取一些东西。但是,没有打印任何内容。

我还尝试了find_all()find() 函数,但我无法提取我需要的内容,甚至无法提取我需要的内容。

我的问题是由新表格的 HTML 结构引起的。它看起来像:

<table ... >
<tbody>
.
.
.    
<tr>
    <th class="a-color-secondary a-size-base prodDetSectionEntry">
        Best Sellers Rank
    </th>
    <td>
         <span>

                <span>#8,740 in Toys &amp; Games (<a href="/gp/bestsellers/toys-and-games/ref=pd_dp_ts_toys-and-games_1">See Top 100 in Toys &amp; Games</a>)</span>
        <br>

                <span>#67 in <a href="/gp/bestsellers/toys-and-games/ref=pd_zg_hrsr_toys-and-games_1_1">Toys &amp; Games</a> &gt; <a href="/gp/bestsellers/toys-and-games/166359011/ref=pd_zg_hrsr_toys-and-games_1_2">Puzzles</a> &gt; <a href="/gp/bestsellers/toys-and-games/166363011/ref=pd_zg_hrsr_toys-and-games_1_3_last">Jigsaw Puzzles</a></span>
        <br>

                <span>#87 in <a href="/gp/bestsellers/toys-and-games/ref=pd_zg_hrsr_toys-and-games_2_1">Toys &amp; Games</a> &gt; <a href="/gp/bestsellers/toys-and-games/251909011/ref=pd_zg_hrsr_toys-and-games_2_2">Preschool</a> &gt; <a href="/gp/bestsellers/toys-and-games/251910011/ref=pd_zg_hrsr_toys-and-games_2_3">Pre-Kindergarten Toys</a> &gt; <a href="/gp/bestsellers/toys-and-games/251942011/ref=pd_zg_hrsr_toys-and-games_2_4_last">Puzzles</a></span>
        <br>

        </span>
    </td>
    </tr>
.
. 
.
</tbody>
</table>

如果我只想提取“Toys & Games > Puzzles > Jigsaw Puzzles”的卖家排名,我应该怎么做? (第二个中的文本,至少在这种情况下,在上面的 HTML 中)

【问题讨论】:

    标签: python web-scraping beautifulsoup amazon


    【解决方案1】:

    我可以通过一些小的调整让你的代码工作:

    1. 去掉soup.select中的'tbody',是浏览器生成的标签
    2. 打印tr.text 而不是text.tr

    代码:

    for tr in soup.select('table#productDetails_detailBullets_sections1 tr'):
        if 'Jigsaw Puzzles' in tr.text :
            print(tr.text.strip())
    

    或者如果你更喜欢find / find_all

    for tr in soup.find('table', id='productDetails_detailBullets_sections1').find_all('tr') :
        if 'Jigsaw Puzzles' in tr.text : 
            for span in tr.find('span').find_all('span') : 
                if 'Jigsaw Puzzles' in span.text : 
                    print(span.text.strip())
    

    【讨论】:

    • 是的,这行得通。但是有没有什么方法可以得到“#89 in Toys & Games > Puzzles > Jigsaw Puzzles”?
    • 谢谢你的作品。很奇怪。当我多次运行该程序时,它会打印“#103 in Toys & Games > Puzzles > Jigsaw Puzzles”或“#89 in Toys & Games > Puzzles > Jigsaw Puzzles”。你有任何资料可以详细解释这个或与这个类似的程序吗?
    • 是的,我也注意到了
    • 一定是 amazon.com 的问题?你能解释一下for span in tr.find('span').find_all('span'):吗?
    • 好的,tr.find('span') 选择第一个 'span' ,然后 find_all('span') 选择所有子跨度
    猜你喜欢
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多