大佬教程收集整理的这篇文章主要介绍了Python Selenium,检查 <div ...> 是否包含网页抓取代码中的单词,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 SELEnium 和 BeautifulSoup 运行一个抓取工具,我想检查某个词是否在
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-asin
。 div 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=""
并检查它是否为空字符串。在这种情况下它不会为空,因为我们有:<div data-asin="0974158216"
和 <div data-asin="1433692163"
我想使用 for 循环或 try/except,但我对 SELEnium 和 HTML 很陌生,我不知道如何解决这个问题。任何形式的帮助将不胜感激。
要搜索带有非空 <div>
的 data-asin="..."
,您可以使用以下示例:
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
以上是大佬教程为你收集整理的Python Selenium,检查 <div ...> 是否包含网页抓取代码中的单词全部内容,希望文章能够帮你解决Python Selenium,检查 <div ...> 是否包含网页抓取代码中的单词所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。