Home > OS >  Simple web-scraping code won't run but no error
Simple web-scraping code won't run but no error

Time:04-20

First post here! I can't seem to get this code to work

python script on VScode

All I did was try to work step by step with this tutorial

https://www.youtube.com/watch?v=RvCBzhhydNk

https://github.com/Vidito/webscraping_housing/blob/main/main.py

but when I run the code, the terminal just gives me nothing (please check image). I am baffled and I am not sure what I am doing wrong. Any help would be appreciated!

from bs4 import BeautifulSoup
import requests

url= "https://www.pararius.com/apartments/amsterdam?ac=1"
page = requests.get(url)

soup = BeautifulSoup(page.content, 'html.parser')
lists = soup.find_all('section', class_="listing-search-item")

for list in lists:
    title = list.find('a', class_="listing-search-item__link--title").text.replace('\n', '')
    location = list.find('div', class_="listing-search-item__location").text.replace('\n', '')
    price = list.find('span', class_="listing-search-item__price").text.replace('\n', '')
    area = list.find('span', class_="illustrated-features__description").text.replace('\n', '')
    
    info = [title, location, price, area]
    print(info)```

Nana

CodePudding user response:

It doesn't print anything, because the find_all function doesn't find anything. The class for the section is called "listing-search-item listing-search-item--list listing-search-item--for-rent" not only listing-search-item. You can use attribute selectors to match everything that starts with what you have right now.

Don't use "list" as a variable name, there is already a built-in function called list.

CodePudding user response:

This is because you have run across fairlane before you get the texts from the site https://www.pararius.com/apartments/amsterdam, it's in order to check whether you a robot.

You can add print(soup) after soup = BeautifulSoup(page.content, 'html.parser'). You will find the outputs were not you are looking for.

  • Related