如何 fetch/scrape html "class" 中 "span" 中的所有元素?
How to fetch/scrape all elements from a html "class" which is inside "span"?
我正在尝试从一个网站上抓取数据,我正在使用这个从“class”下的所有元素收集数据,这些元素位于“跨度”内代码。但是我最终只获取了一个元素而不是所有元素。
expand_hits = soup.findAll("a", {"class": "sold-property-listing"})
apartments = []
for hit_property in expand_hits:
#element = soup.findAll("div", {"class": "sold-property-listing__location"})
place_name = expand_hits[1].find("div", {"class": "sold-property-listing__location"}).findAll("span", {"class": "item-link"})[1].getText()
print(place_name)
apartments.append(final_str)
打印的预期结果(place_name)
Stockholm
Malmö
Copenhagen
...
..
.
打印的结果(place_name)
Malmö
Malmö
Malmö
...
..
.
当我尝试从 expand_hits[1] 获取内容时,我只得到一个元素。如果我不指定索引抓取器会抛出有关使用 find()、find_all() 和 findAll() 的错误。据我所知,我认为我必须迭代地调用元素的内容。
非常感谢任何帮助。
提前致谢!
使用循环变量而不是索引到具有相同索引 (expand_hits[1]) 的相同 collection 并附加 place_name 而不是 final_str
expand_hits = soup.findAll("a", {"class": "sold-property-listing"})
apartments = []
for hit_property in expand_hits:
place_name = hit_property.find("div", {"class": "sold-property-listing__location"}).find("span", {"class": "item-link"}).getText()
print(place_name)
apartments.append(place_name)
你只需要查找而不需要索引
添加User-Agentheader以确保结果。另外,我注意到我必须选择一个 parent 节点,因为使用 class item-link 至少不会捕获一个结果,例如Övägen 6C
。由于现在选择 parent node.
,我使用 replace 来去除隐藏的文本
from bs4 import BeautifulSoup
import requests
import re
url = "https://www.hemnet.se/salda/bostader?location_ids%5B%5D=474035"
page = requests.get(url, headers = {'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(page.content,'html.parser')
for result in soup.select('.sold-results__normal-hit'):
print(re.sub(r'\s{2,}',' ', result.select_one('.sold-property-listing__location h2 + div').text).replace(result.select_one('.hide-element').text.strip(), ''))
如果您只想在马尔默的某个地方,例如 Limhamns Sjöstad
,您需要检查每个列表有多少 child 个 span 标签。
for result in soup.select('.sold-results__normal-hit'):
nodes = result.select('.sold-property-listing__location h2 + div span')
if len(nodes)==2:
place = nodes[1].text.strip()
else:
place = 'not specified'
print(place)
我正在尝试从一个网站上抓取数据,我正在使用这个从“class”下的所有元素收集数据,这些元素位于“跨度”内代码。但是我最终只获取了一个元素而不是所有元素。
expand_hits = soup.findAll("a", {"class": "sold-property-listing"})
apartments = []
for hit_property in expand_hits:
#element = soup.findAll("div", {"class": "sold-property-listing__location"})
place_name = expand_hits[1].find("div", {"class": "sold-property-listing__location"}).findAll("span", {"class": "item-link"})[1].getText()
print(place_name)
apartments.append(final_str)
打印的预期结果(place_name)
Stockholm
Malmö
Copenhagen
...
..
.
打印的结果(place_name)
Malmö
Malmö
Malmö
...
..
.
当我尝试从 expand_hits[1] 获取内容时,我只得到一个元素。如果我不指定索引抓取器会抛出有关使用 find()、find_all() 和 findAll() 的错误。据我所知,我认为我必须迭代地调用元素的内容。
非常感谢任何帮助。 提前致谢!
使用循环变量而不是索引到具有相同索引 (expand_hits[1]) 的相同 collection 并附加 place_name 而不是 final_str
expand_hits = soup.findAll("a", {"class": "sold-property-listing"})
apartments = []
for hit_property in expand_hits:
place_name = hit_property.find("div", {"class": "sold-property-listing__location"}).find("span", {"class": "item-link"}).getText()
print(place_name)
apartments.append(place_name)
你只需要查找而不需要索引
添加User-Agentheader以确保结果。另外,我注意到我必须选择一个 parent 节点,因为使用 class item-link 至少不会捕获一个结果,例如Övägen 6C
。由于现在选择 parent node.
from bs4 import BeautifulSoup
import requests
import re
url = "https://www.hemnet.se/salda/bostader?location_ids%5B%5D=474035"
page = requests.get(url, headers = {'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(page.content,'html.parser')
for result in soup.select('.sold-results__normal-hit'):
print(re.sub(r'\s{2,}',' ', result.select_one('.sold-property-listing__location h2 + div').text).replace(result.select_one('.hide-element').text.strip(), ''))
如果您只想在马尔默的某个地方,例如 Limhamns Sjöstad
,您需要检查每个列表有多少 child 个 span 标签。
for result in soup.select('.sold-results__normal-hit'):
nodes = result.select('.sold-property-listing__location h2 + div span')
if len(nodes)==2:
place = nodes[1].text.strip()
else:
place = 'not specified'
print(place)