I’m trying to learn beatifulsoup (and python as a whole, pretty much still a beginner) and playing around with how to use it properly. I notice that when I scrape the website I’m testing for data from the search results, it lists it 3 times.
Specifically, I’m trying to output the title, link, and price of the real estate property from the website. The price doesn’t seem to duplicate while the title and link does. Can’t really figure out if it’s because of my code or something with the website itself.
JavaScript
x
17
17
1
import requests
2
from bs4 import BeautifulSoup
3
4
userSearch = input('Input search: ')
5
link = 'https://www.lamudi.com.ph/buy/?q={}'.format(userSearch)
6
page = requests.get(link)
7
soup = BeautifulSoup(page.content, 'html.parser')
8
9
titleList = soup.find_all("a", title=True)
10
priceList = soup.find_all("span", class_="PriceSection-FirstPrice", text=True)
11
12
for (i,j) in zip(titleList, priceList):
13
print(i['title'])
14
print(i['href'])
15
print(j.get_text())
16
print("===============")
17
Output would be something like this where the price doesn’t match the listing because of the duplicated info:
JavaScript
1
36
36
1
Input Property search: manila
2
Suntrust Solana in Ermita Manila 3 Bedroom Unit for Sale
3
https://www.lamudi.com.ph/suntrust-solana-in-ermita-manila-3-bedroom-unit-for-sale.html
4
5
₱ 6,672,197
6
7
===============
8
Suntrust Solana in Ermita Manila 3 Bedroom Unit for Sale
9
https://www.lamudi.com.ph/suntrust-solana-in-ermita-manila-3-bedroom-unit-for-sale.html
10
11
₱ 6,888,800
12
13
===============
14
Suntrust Solana in Ermita Manila 3 Bedroom Unit for Sale
15
https://www.lamudi.com.ph/suntrust-solana-in-ermita-manila-3-bedroom-unit-for-sale.html
16
17
₱ 168,000,000
18
19
===============
20
3 Bedroom Unit For Sale in Suntrust Solana Manila- SOLA-01-21-H
21
https://www.lamudi.com.ph/3-bedroom-unit-for-sale-in-suntrust-solana-manila-sola-01-21-h.html
22
23
₱ 53,000,000
24
25
===============
26
3 Bedroom Unit For Sale in Suntrust Solana Manila- SOLA-01-21-H
27
https://www.lamudi.com.ph/3-bedroom-unit-for-sale-in-suntrust-solana-manila-sola-01-21-h.html
28
29
₱ 53,000,000
30
31
===============
32
3 Bedroom Unit For Sale in Suntrust Solana Manila- SOLA-01-21-H
33
https://www.lamudi.com.ph/3-bedroom-unit-for-sale-in-suntrust-solana-manila-sola-01-21-h.html
34
35
₱ 46,500,000
36
Advertisement
Answer
You should iterate row-by-row. It’s safer than using zip()
.
To get all titles, links + prices you can use next example:
JavaScript
1
20
20
1
import requests
2
from bs4 import BeautifulSoup
3
4
userSearch = "manila"
5
link = "https://www.lamudi.com.ph/buy/?q={}".format(userSearch)
6
7
soup = BeautifulSoup(requests.get(link).content, "html.parser")
8
9
10
for row in soup.select(".ListingCell-row"):
11
title = row.h2.get_text(strip=True)
12
link = row.a["href"]
13
price = row.select_one(
14
".PriceSection-FirstPrice, .PriceSection-NoPrice"
15
).get_text(strip=True)
16
print(title)
17
print(link)
18
print(price)
19
print("=" * 80)
20
Prints:
JavaScript
1
23
23
1
Suntrust Solana in Ermita Manila 3 Bedroom Unit for Sale
2
https://www.lamudi.com.ph/suntrust-solana-in-ermita-manila-3-bedroom-unit-for-sale.html
3
₱ 6,672,197
4
================================================================================
5
3 Bedroom Unit For Sale in Suntrust Solana Manila- SOLA-01-21-H
6
https://www.lamudi.com.ph/3-bedroom-unit-for-sale-in-suntrust-solana-manila-sola-01-21-h.html
7
₱ 6,888,800
8
================================================================================
9
Rare Sale!!Prime Commercial Property strategically in Paco, Manila
10
https://www.lamudi.com.ph/rare-sale-prime-commercial-property-strategically-in-paco-manila.html
11
₱ 168,000,000
12
================================================================================
13
A Luxurious Unit E Townhouse For Sale in A Peaceful Neighborhood in Paco Manila
14
https://www.lamudi.com.ph/a-luxurious-unit-e-townhouse-for-sale-in-a-peaceful-neighborhood-in-paco-manila.html
15
₱ 53,000,000
16
================================================================================
17
For Sale Luxurious 4-Bedroom Townhouse in a Peaceful Neighborhood in Paco Manila
18
https://www.lamudi.com.ph/for-sale-luxurious-4-bedroom-townhouse-in-a-peaceful-neighborhood-in-paco-manila.html
19
₱ 53,000,000
20
================================================================================
21
22
23