Data scraping with python beautifulsoup

58 Views Asked by At

Hey I created a script to datascrape a specific website, the code is ok but i get a error when i try to excecute the code.

Tried cmd to excecute the file so i can get results but still a error.

NameError: name 'scrape' is not defined

''scrape.py'' File "", line 1

import requests 
from bs4 import BeautifulSoup 

url = "sawadee.nl/groepsrondreizen/midden-en-zuid-amerika/costa-rica" 
response = requests.get(url) 
soup = BeautifulSoup(response.text, "html.parser") 
for row in soup.select(".departure-list__table tbody tr"): 
    date = row.select_one(".departure-list__date").text.strip() 
    group = row.select_one(".departure-list__group").text.strip() 
    print(f"Op {date} is de groepssamenstelling: {group}")
3

There are 3 best solutions below

2
Moshe Eichler On

How did you run the script?

If you run it from the terminal you have to run something like this:

python file_name.py
1
Help On

I tried to run it from cmd / python because i used Pycharm to create the script. Outcome of the script is a list with the information that I need in a excel file or csv.

I ran the script as: python scrape.py that failed.

6
Kulasangar On

I tried to run your same code, but a small change by adding the https in front of the url. I was getting 200 in the response but the in the soup.select its failing due to some html parsing. You may have to check that cos I don't see a departure-list in any of the html tags:

import requests
from bs4 import BeautifulSoup

url = "https://sawadee.nl/groepsrondreizen/midden-en-zuid-amerika/costa-rica"
response = requests.get(url). ### getting 200 response here
soup = BeautifulSoup(response.text, "html.parser")
for row in soup.select(".departure-list__table tbody tr"):
    date = row.select_one(".departure-list__date").text.strip()
    group = row.select_one(".departure-list__group").text.strip()
    print(f"Op {date} is de groepssamenstelling: {group}")

enter image description here