【问题标题】:How can I scrape a href that is hidden behind a placeholder?如何抓取隐藏在占位符后面的 href?
【发布时间】:2021-03-29 12:21:42
【问题描述】:

我正在尝试从网站上抓取以下 href。网站上有几个我打算抓取的href,因此我正在循环浏览该网站,以便将它们全部存储在一个列表中。以下是其中一个 href 的示例。

<div class="col-md-4 h-gutter">
   <div class="product box" data-productid="2111214"> 
      <a href="/products/examples/product1/"> 
         <h3>Product 1</h3> 
         <div class="product-small-text">

这是我有问题的代码部分。注释掉的是我试图只收集hrefs。由于这不起作用,现在我正试图刮掉整个“col-md-4 h-gutter”

for product in soup.select('div.product.box'):
    link.append(product)
    #link.append(product.a['href'])

print(link)

下面是打印到终端的内容。如您所见,href 隐藏在占位符后面。

</div>, <div class="product placeholder-container box"> 
<h3><span class="placeholder-text--long"></span></h3> 
<div class="product-small-text"> 
<span class="placeholder-text--short"></span> 
</div>

如何打印出 href 的值?

【问题讨论】:

  • 所有相关的a href 是否都以/products/examples/ 开头?也许只搜索这些更容易。否则你需要做一个更复杂的选择器。
  • @tobias 是的,他们这样做了。但是,我还需要解析网站上的其他信息,这些信息我没有在这里显示,这让事情变得很困难。在任何情况下如何最好地搜索 /products/?
  • [href*='/products/examples'][href^='/products/examples']
  • 用你最初的方法,你可以试试.select('.h-gutter &gt; .product .box')。它只是一个 CSS 选择器。
  • 是的,您可以这样做。使用 F12 打开浏览器的网络选项卡,然后按 F5 刷新页面,然后查看该 API 调用的网络活动,并查看您的区域设置的参数。

标签: python python-3.x web-scraping beautifulsoup web-scraping-language


【解决方案1】:

使用 json 响应要容易得多。如果您需要表格形式,只需将其输入 pandas:

import requests
import pandas as pd

url = 'https://www.masterofmalt.com/api/v2/lightningdeals/?isVatableCountry=1&deliveryCountryId=464&filter=nodrams&_=1617024330709&format=json'
headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'}
jsonData = requests.get(url, headers=headers).json()

df = pd.DataFrame(jsonData['lightningDeals'])

输出:前 5 行,共 43 行

print(df.head(5).to_string())
                                                              productUrl                                                                         productImageUrl  productRating  productReviewCount  productVolume  productAbv                categories                   endDateUtc  productId              productName  dealPrice  previousPrice  timeRemaining  saving  percentageClaimed  isActive  dailyDeal
0                      /whiskies/tobermory/tobermory-12-year-old-whisky/                      /whiskies/p-IMAGEPRESET/tobermory/tobermory-12-year-old-whisky.jpg            5.0                  17             70        46.3   [Whiskies, Single Malt]  2021-04-04T22:57:00.0000000      87989    Tobermory 12 Year Old      34.85          39.85         550379     5.0           0.669725      True      False
1  /whiskies/elements-of-islay/peat-pure-islay-elements-of-islay-whisky/  /whiskies/p-IMAGEPRESET/elements-of-islay/peat-pure-islay-elements-of-islay-whisky.jpg            0.0                   0             50        45.0  [Whiskies, Blended Malt]  2021-04-04T22:59:00.0000000      58061          Peat Pure Islay      23.94          28.94         550499     5.0           0.625000      True      False
2                                 /mezcal/ilegal/ilegal-reposado-mezcal/                                 /mezcal/p-IMAGEPRESET/ilegal/ilegal-reposado-mezcal.jpg            5.0                   3             70        40.0        [Mezcal, Reposado]  2021-04-04T22:59:00.0000000       9277          Ilegal Reposado      53.40          59.40         550499     6.0           0.500000      True      False
3                        /whiskies/nikka/nikka-coffey-grain-whisky-70cl/                        /whiskies/p-IMAGEPRESET/nikka/nikka-coffey-grain-whisky-70cl.jpg            4.5                  40             70        45.0         [Whiskies, Grain]  2021-04-04T22:57:00.0000000      32316  Nikka Coffey Grain 70cl      49.83          54.83         550379     5.0           0.410256      True      False
4                              /rum/satchmo/satchmo-mojito-spirited-rum/                              /rum/p-IMAGEPRESET/satchmo/satchmo-mojito-spirited-rum.jpg            5.0                  14             70        37.5             [Rum, Spiced]  2021-04-04T22:58:00.0000000     106576              Satchmo Rum      34.95          39.95         550439     5.0           0.338710      True      False

【讨论】:

  • 抱歉,错误已修复。如何仅提取产品 Url、dealPrice、保存并存储在列表中?
  • 试试里面的 headers 参数
  • 已修复,谢谢。如何仅提取产品 Url、dealPrice、保存并存储在列表中?我不需要存储为表格,只需将这些特定参数存储在一个列表(或单独的列表)中。
  • 使用 pandas 对其进行切片。即list(df['productUrl']).
猜你喜欢
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 2015-09-17
  • 2021-09-11
  • 2013-06-05
  • 2016-09-14
相关资源
最近更新 更多