【发布时间】:2019-01-17 10:44:20
【问题描述】:
我正在尝试从所有项目都像这样存储的页面中转义数据
<div class="box browsingitem canBuy 123"> </div>
<div class="box browsingitem canBuy 264"> </div>
有数百个,但是当我尝试将它们添加到数组中时,它只节省了 24 个
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re
import lxml
my_url = 'https://www.alza.co.uk/tablets/18852388.htm'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "lxml")
classname = "box browsingitem"
containers = page_soup.find_all("div", {"class":re.compile(classname)})
#len(containers) will be equal to 24
for container in containers:
title_container = container.find_all("a",{"class":"name browsinglink"})
product_name = title_container[0].text
print("product_name: " + product_name)
re.compile 有问题吗?我还能如何搜索课程?
感谢您的帮助
【问题讨论】:
-
是否有可能在滚动时加载了这数百个?
-
如果所有这些项目都包含类名
box browsingitem,为什么不直接使用page_soup.find_all('div', 'box browsingitem')。这应该检索加载到 DOM 中的该类的所有项目。 -
@taras 它是,但它加载 24,例如,即使有 18 个项目......真的很奇怪
-
@Steven 由于某种原因它不起作用,它加载 0
-
你能提供你想抓取的链接吗? @布莱斯
标签: python html web-scraping beautifulsoup html-parsing