【发布时间】:2019-03-07 06:51:58
【问题描述】:
我试图提取运行代码返回的 h1 标记中的文本,但没有收到任何输出。但是,代码能够找到指定的标签如下:
<h1 class="product-name main-heading">Mixed Brown Rice 2.5kg</h1>
网页链接:
https://giantonline.com.sg/product/mixed-brown-rice-5142760
这是我使用的代码:
driver.get("https://giantonline.com.sg/product/mixed-brown-rice-5142760")
driver.implicitly_wait(30)
time.sleep(4)
bs2=BeautifulSoup(driver.page_source, 'lxml')
for z in bs2.find_all('div',class_="col-md-5 col-sm-5 col-xs-12"):
try:
name = z.find('h1',class_='product-name')
print(type(name))
print(name)
name = name.get_text(seperator=' ')
print(name)
size = z.find('h1',class_='product-size main-heading')
size = size.text
oldprice = z.find('div',class_='old-price')
oldprice = oldprice.text
price = z.find('div',class_='content_price')
price = price.text
except:
continue
为什么我无法从h1 标签中取出文本?
【问题讨论】:
标签: python html beautifulsoup