在使用Beautiful Soup的Python中使用multiple for循环
下面的代码是用于获取1项的测试在使用Beautiful Soup的Python中使用multiple for循环,python,for-loop,beautifulsoup,Python,For Loop,Beautifulsoup,下面的代码是用于获取1项的测试 from bs4 import BeautifulSoup as bs import requests import pandas as pd url = "https://www.property24.com/for-sale/woodland-hills-wildlife-estate/bloemfontein/free-state/10467/109825373" data = requests.get(url) soup = bs(
from bs4 import BeautifulSoup as bs
import requests
import pandas as pd
url = "https://www.property24.com/for-sale/woodland-hills-wildlife-estate/bloemfontein/free-state/10467/109825373"
data = requests.get(url)
soup = bs(data.content,"html.parser")
下面的代码是我们必须获得所有col-xs-6 p24_propertyOverviewKey的代码
property_overview = soup.find(class_="p24_regularListing").find(class_="p24_propertyOverview").find(class_='p24_propertyOverviewRow').find(class_='col-xs-6 p24_propertyOverviewKey').text
property_overview
Output : 'Listing Number'
上述代码仅输出1项。并非所有的更简单地说,您可以使用
soup.select()
(然后通过注释,您可以使用.get_text()
从每个标记中提取文本)
让BeautifulSoup做硬循环工作会更容易吗?问题是我尝试了“查找”而不是“选择”,你让我的一天变得轻松多了。然后您将如何在文本中获得此输出?添加了一个真实的答案。:)
p24_regularListing_items = soup.find_all(class_="p24_regularListing")
for p24_propertyOverview_item in p24_regularListing_items:
p24_propertyOverview_items = p24_propertyOverview_item.find_all(class_="p24_propertyOverview")
for p24_propertyOverviewRow_item in p24_propertyOverview_items:
p24_propertyOverviewRow_items = p24_propertyOverviewRow_item.find_all(class_="p24_propertyOverviewRow")
for p24_propertyOverviewKey_item in p24_propertyOverviewRow_items:
p24_propertyOverviewKey_items = p24_propertyOverviewKey_item.find_all(class_="col-xs-6 p24_propertyOverviewKey")
p24_propertyOverviewKey_items
from bs4 import BeautifulSoup
import requests
resp = requests.get(
"https://www.property24.com/for-sale/woodland-hills-wildlife-estate/bloemfontein/free-state/10467/109825373"
)
resp.raise_for_status()
soup = BeautifulSoup(resp.content, "html.parser")
texts = []
for tag in soup.select(
# NB: this selector uses Python's implicit string concatenation
# to split it onto several lines.
".p24_regularListing "
".p24_propertyOverview "
".p24_propertyOverviewRow "
".p24_propertyOverviewKey"
):
texts.append(tag.get_text())
print(texts)