无法使用 Beautifulsoup 正确抓取 <strong> 标签

它的DV7

所以我试图使用以下代码从这个阿迪达斯网站上抓取产品的日期

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/'
                         '84.0.4147.105 Safari/537.36'}
url = "https://www.adidas.com.sg/release-dates"
productsource = requests.get(url, headers=headers, timeout=15)
productinfo = BeautifulSoup(productsource.text, "lxml")


def jdMonitor():
    # webscraper
    all_items = productinfo.find_all(name="div", class_="gl-product-card")
    # print(all_items)
    for item in all_items:
        # print(item)
        pname = item.find(name="div", class_="plc-product-name___2cofu").text
        pprice = item.find(name="div", class_="gl-price-item").text
        imagelink = item.find(name="img")['src']
        plink = f"https://www.adidas.com.sg/{item.a['href']}"
        try:
            pdate = item.find(name="div", class_="plc-product-date___1zgO_").strong.text
        except AttributeError as e:
            print(e)
            pdate = "Data Not Available"
        print(f"""
        Product Name: {pname}
        Product Price: {pprice}
        Image Link: {imagelink}
        Product Link: {plink}
        Product Date: {pdate}
""")


jdMonitor()

但是我在pdate. 但是如果我print(productinfo.find_all(name="strong"))用来提取页面上的所有强标签,我就能够正确提取所有标签,而不是我需要的标签。我得到的输出为:

... <strong>All Recycled Materials</strong>, <strong> </strong> ...

第二对强标签之间的空白区域应包含日期,如

<strong>Wednesday 30 Jun 21:30</strong>

有人可以解释为什么会这样吗?以及提取它的方法。

迪米特里·祖布

似乎日期是动态更新的,并且源代码中没有这样的日期(打开源代码并查找“WEDNESDAY 30 JUN 19:00”,什么都不会显示)。最明显的事情是使用selenium使其工作,但这可能是一个缓慢的解决方案。requests-html对我不起作用,就像bs4. 呈现页面也无济于事(或者我做错了什么)。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
# running in headless mode for some reason gives no result or throws an error.
# options.headless = True 
driver = webdriver.Chrome(options=options)
driver.get('https://www.adidas.com.sg/release-dates')

for date in driver.find_elements_by_css_selector('.plc-product-date___1zgO_.gl-label.gl-label--m.gl-label--condensed'):
    print(date.text)
driver.quit()

# output:
'''
WEDNESDAY 30 JUN 19:00
THURSDAY 01 JUL 05:00
THURSDAY 01 JUL 05:00
THURSDAY 01 JUL 05:00
THURSDAY 01 JUL 05:00
THURSDAY 01 JUL 05:00
THURSDAY 01 JUL 05:00
THURSDAY 01 JUL 05:00
'''

您还可以regex像这样获取此日期(如果它们会出现):

import re

test = '''
Wednesday 30 Jun 19:00
THURSDAY 01 JUL 05:00
THURSDAY 01 FEb 25:00
'''
matches = re.findall(r"[a-zA-Z]+\s\d+\s\w+\s\d+:\d+", str(test))
finall_matches = '\n'.join(matches)
print(finall_matches)

# output before joining: "['Wednesday 30 Jun 19:00', 'THURSDAY 01 JUL 05:00', 'THURSDAY 01 FEb 25:00']"

# output after joining:
'''
Wednesday 30 Jun 19:00
THURSDAY 01 JUL 05:00
THURSDAY 01 FEb 25:00
'''

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用BeautifulSoup和Selenium抓取特定的html标签

如何使用BeautifulSoup抓取<h1>标签?[蟒蛇]

使用正确的字符编码进行抓取(python请求+ beautifulsoup)

使用BeautifulSoup抓取时,不输出带有正确标签的None

使用BeautifulSoup抓取预标签内的文本

如何使用beautifulSoup从<td>标签分别抓取数据?

使用replace从html中删除<strong>标签

无法使用BeautifulSoup抓取嵌套标签

beautifulsoup无法正确解析xml标签,但是lxml是

在Python标签中使用BeautifulSoup进行网络抓取

无法使用BeautifulSoup4(初学者)抓取正确的Wikitable

使用beautifulsoup从脚本标签中抓取数据

如何从使用BeautifulSoup抓取的列表中删除标签?

无法使用BeautifulSoup获取锚标签

如何使用BeautifulSoup抓取缺少标签的网页

如何使用beautifulsoup在span标签之间抓取

为什么我不能使用BeautifulSoup抓取某些标签?

使用 Python 和 BeautifulSoup 抓取 alt 标签

使用 python 和 BeautifulSoup 抓取不完整的标签

使用 Python、Selenium 和 BeautifulSoup 来抓取标签的内容?

使用 BeautifulSoup 从封装的 <strong> 标签中获取内容

使用 BeautifulSoup 进行网页抓取时无法在 a 标签中显示文本

使用 BeautifulSoup 来抓取带有 CSS ID 的标签

使用 rvest 获取正确的网页抓取标签

如何使用 BeautifulSoup 在“ul”标签下抓取数组的值?

Python用beautifulsoup抓取无法正确抓取某些数据行

抓取标签属性 BeautifulSoup

使用 BeautifulSoup 提取抓取网页的 Script 标签所需的信息

无法使用 BeautifulSoup 从隐藏标签中抓取“价值”