使用BeautifulSoup进行抓取时,输出带有正确标记的None

2024-06-02 04:24:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从Zalora获取以下三个内容: 1商品品牌 2项目名称 三。项目价格(旧)

以下是我的初步尝试:

from bs4 import BeautifulSoup
import requests

def make_soup(url):
    html = requests.get(url)
    bsObj = BeautifulSoup(html.text, 'html.parser')
    return bsObj

soup = make_soup('https://www.zalora.com.hk/men/clothing/shirt/?gender=men&dir=desc&sort=popularity&category_id=31&enable_visual_sort=1')

itemBrand = soup.find("span",{"class":"b-catalogList__itmBrand fsm txtDark uc js-catalogProductTitle"})
itemName = soup.find("em",{"class":"b-catalogList__itmTitle fss"})
itemPrice = soup.find("span",{"class":"b-catalogList__itmPrice old"})

print(itemBrand, itemName, itemPrice)

输出:

^{pr2}$

然后我做进一步的调查:

productsCatalog = soup.find("ul",{"id":"productsCatalog"})
print(productsCatalog)

输出:

<ul class="b-catalogList__wrapper clearfix" id="productsCatalog">

这是一件让我困惑的奇怪的事情,ul标签中应该有很多标签(我需要的3件东西都在那些隐藏的标签中),为什么它们没有出现?在

事实上,我在ul标记中尝试使用beauthoulsoup来获取的所有内容都没有输出。在


Tags: importidurl内容makehtml标签find
1条回答
网友
1楼 · 发布于 2024-06-02 04:24:04

由于此内容是由JavaScript呈现的,因此不能使用requests模块访问它。您应该使用selenium使浏览器自动化,然后使用BeautifulSoup来解析实际的{}。在

这是使用selenium和{a1}一起使用的方法:

from selenium import webdriver
from bs4 import BeautifulSoup

chrome_driver = "path\\to\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver)

target = 'https://www.zalora.com.hk/men/clothing/shirt/?gender=men&dir=desc&sort=popularity&category_id=31&enable_visual_sort=1'
driver.get(target)

soup = BeautifulSoup(driver.page_source, "lxml")

print(soup.find("span",{"class":"b-catalogList__itmBrand fsm txtDark uc js-catalogProductTitle"}).get_text().strip())
print(soup.find("span", {'class': 'b-catalogList__itmPrice old'}).get_text().strip())
print(soup.find("em",{"class":"b-catalogList__itmTitle fss"}).get_text().strip())

输出:

^{pr2}$

相关问题 更多 >