【问题标题】:Python Selenium, check if <div ...> contains a word in web-scraping codePython Selenium,检查 <div ...> 是否包含网络抓取代码中的单词
【发布时间】:2021-07-22 01:29:23
【问题描述】:

我正在使用 Selenium 和 BeautifulSoup 运行刮板,我想检查某个单词是否在

中。

一个sn-p的HTML代码如下:

<div data-asin="0974158232" data-index="0" data-uuid="1f362f6b-dde2-4377-a5f3-518513486b7d" data-component-type="s-search-result" class="s-result-item s-asin sg-col-0-of-12 sg-col-16-of-20 sg-col sg-col-12-of-16" data-component-id="14" data-cel-widget="search_result_0"><div class="sg-col-inner">
<div data-asin="" data-index="1" class="a-section a-spacing-none s-result-item s-flex-full-width s-border-bottom-none s-widget" data-cel-widget="search_result_1">
<div data-asin="" data-index="2" class="a-section a-spacing-none s-result-item s-flex-full-width s-border-bottom-none s-widget" data-cel-widget="search_result_2">

首先,我想检查div data-asin="" 是否为空,或者是否存在data-asin="0974158232" 中的字符串。

如果它是空的,我想输入

并查找data-asindiv data-asin="" data-index="2" 的一个例子是:
> <div data-asin="" data-index="2" class="a-section a-spacing-none s-result-item s-flex-full-width s-border-bottom-none s-widget" data-cel-widget="search_result_2">
> <span cel_widget_id="MAIN-SEARCH_RESULTS-2" class="celwidget slot=MAIN template=SEARCH_RESULTS 
  widgetId=fkmr-search-results" data-csa-c-id="9so6vg-imque6-h59746-o5az71" data-cel-widget="MAIN- 
  SEARCH_RESULTS-2">
    > <div class="s-result-list sg-row">
       > <div class="s-result-item sg-col-16-of-20 sg-col sg-col-8-of-12 sg-col-12-of-16" data-cel- 
         widget="search_result_3">
       > <div data-asin="0974158216" data-index="0" data-uuid="99a1b582-2fcb-49b8-8d13-739783e460a5" 
         data-component-type="s-search-result" class="s-result-item s-asin sg-col-0-of-12 sg-col-16- 
         of-20 sg-col sg-col-12-of-16" data-component-id="15" data-cel-widget="search_result_4"><div 
         class="sg-col-inner">
       > <div data-asin="1433692163" data-index="1" data-uuid="8f8bfb8c-6083-4c26-bdd5-3032bcfe4bed" 
         data-component-type="s-search-result" class="s-result-item s-asin sg-col-0-of-12 sg-col-16- 
         of-20 sg-col sg-col-12-of-16" data-component-id="16" data-cel-widget="search_result_5">

在这里,我想告诉代码查找data-asin="" 并检查它是否为空字符串。在这种情况下,它不会为空,因为我们有:&lt;div data-asin="0974158216"&lt;div data-asin="1433692163"

我在考虑使用 for 循环或 try/except,但我对 Selenium 和 HTML 很陌生,我不知道如何解决这个问题。任何形式的帮助将不胜感激。

【问题讨论】:

  • 可以分享网址吗?可以用beautifulsoup吗?
  • @AndrejKese 当然,感谢您的回复!网址是link 我有进口美汤是的。从来没有用过很多,但能学到一些东西会很棒,所以请继续。

标签: python html css selenium web-scraping


【解决方案1】:

要使用非空data-asin="..." 搜索&lt;div&gt;,您可以使用以下示例:

import requests
from bs4 import BeautifulSoup


url = "https://www.amazon.com/s?k=A+Biblically+Based+Model+of+Cultural+Competence+in+the+Delivery+of+Healthcare+Services%3A+Seeing&ref=nb_sb_noss"
headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
    "Accept-Language": "en-US,en;q=0.5",
}

soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")

# search only data-asin that have value, print it and the title
for div in soup.find_all("div", {"data-asin": bool}):
    print(div["data-asin"], div.select_one(".a-text-normal").text)

打印:

0974158232 A Biblically Based Model of Cultural Competence in the Delivery of Healthcare Services: Seeing 
1433692163 Planting Missional Churches: Your Guide to Starting Churches that Multiply 
0310341728 Less Than Perfect: Broken Men and Women of the Bible and What We Can Learn from Them 
0800796853 God's Smuggler 
1885904088 The Excellent Wife: A Biblical Perspective 
B07K7YJPXD Hope Channel 
B07F1DNGMS Alistair Begg - Truth For Life 
B07DHZ6DL9 Star Trek Beyond (4K UHD) 
B0010ZONIY Heart of the Ukulele 

【讨论】:

  • 谢谢@AndrejKesely!我将很快对其进行测试,但看起来很有希望。请问您能详细说明两件事吗? 1)我必须通过大量产品运行代码,因此使用 URL 似乎是不可能的。 soup = BeautifulSoup(html) 会成功吗? 2) 能解释一下{"data-asin": bool} 的作用吗?
  • @econnoob5 1.) 您需要以某种方式加载页面,然后将 HTML 源“提供”给 beautifulsoup。您可以使用requestsselenium 来完成。 2.) {"data-asin": bool} 选择所有具有data-asin= 属性的元素,其中bool(&lt;value of data-asin property&gt;) 的计算结果为True。所以它会过滤掉空属性(""
  • 谢谢,非常清楚@AndrejKesely。我正在努力解决的是我没有 URL 列表。我在亚马逊搜索框中输入了一个产品列表,但此时我不知道如何获取 URL 以将 HTML 源提供给 beautifulsoup。你对如何做到这一点有任何想法吗?似乎请求可以解决问题,但我不确定如何
  • @econnoob5 您可以尝试更改URL中的k参数来搜索新产品。
猜你喜欢
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 2020-09-04
  • 1970-01-01
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多