猿问

Python 使用 Selenium 抓取 H2 和价格

我试图从此 html 中抓取产品名称和产品价格


<div class="product-info">

    <div class="left">

        <div class="image a_bossthemes"><a href="/image/cache/data/spigen/airpods-pro/shine-shield/spigenairpodsproshineshieldmetallicsilver1-700x700.jpg" title="" class="colorbox cboxElement"><img src="/image/cache/data/spigen/airpods-pro/shine-shield/spigenairpodsproshineshieldmetallicsilver1-228x228.jpg" title="" alt="" id="image"></a></div>

              <div class="image-additional a_bossthemes">

    <div class="es-carousel">

      <ul class="skin-opencart" style="width: 496px; margin-left: 0px;">

                    <li style="margin-right: 8px; width: 73px;"><div class="boss-image-add"><a href="/image/cache/data/spigen/airpods-pro/shine-shield/spigenairpodsproshineshieldmetallicsilver7-700x700.jpg" title="" class="colorbox cboxElement"><img src="/image/cache/data/spigen/airpods-pro/shine-shield/spigenairpodsproshineshieldmetallicsilver7-74x74.jpg" title="" alt=""></a></div></li>

                    <li style="margin-right: 8px; width: 73px;"><div class="boss-image-add"><a href="/image/cache/data/spigen/airpods-pro/shine-shield/spigenairpodsproshineshieldmetallicsilver6-700x700.jpg" title="" class="colorbox cboxElement"><img src="/image/cache/data/spigen/airpods-pro/shine-shield/spigenairpodsproshineshieldmetallicsilver6-74x74.jpg" title="" alt=""></a></div></li>

                    <li style="margin-right: 8px; width: 73px;"><div class="boss-image-add"><a href="/image/cache/data/spigen/airpods-pro/shine-shield/spigenairpodsproshineshieldmetallicsilver5-700x700.jpg" title="" class="colorbox cboxElement"><img src="/image/cache/data/spigen/airpods-pro/shine-shield/spigenairpodsproshineshieldmetallicsilver5-74x74.jpg" title="" alt=""></a></div></li>


任何人都可以帮助我如何让它发挥作用?有一些解释,这样我就可以解决问题。


桃花长相依
浏览 111回答 1
1回答

慕婉清6462132

要获取产品的名称、价格和描述,请尝试以下操作:import timefrom selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.headless = Falsedriver = webdriver.Chrome(options=options)url = "https://www.unomax.com/apple/anti-dust-sticker-apple-airpods-pro-spigen-shine-shield-metallic-silver-2-sets.html"driver.get(url)time.sleep(2)element = driver.find_element_by_xpath('//*[@class="right"]')name = element.find_element_by_tag_name("h2").textprice = element.find_element_by_class_name("price").textdescription = element.find_element_by_xpath('//*[@style="color: rgb(68, 68, 68); font-family: "]')print(f"{name} - {price.replace('HARGA: ', '')}")print(description.text)driver.close()输出:ANTI DUST STICKER APPLE AIRPODS PRO SPIGEN SHINE SHIELD - METALLIC SILVER (2 SETS) - RP.225,000Spigen Shine Shield Anti Dust Sticker merupakan sticker protector dengan material logam tipis yang dirancang khusus untuk mencegah debu menempel dan goresan pada AirPods Pro anda. Desain modern, simple dan pemasangannya pun sangat mudah dilakukan. Memiliki ketebalan hanya 0.04mm dan memiliki warna silver yang elegan memberikan perlindungan dan kemewahan pada AirPods Pro anda.&nbsp;
随时随地看视频慕课网APP

相关分类

Python
我要回答