Beautifulsoup.py
# -*- coding: utf-8 -*"""
Created on Wed Feb 27 15:02:14 2019
@author: user
"""
import requests
from bs4 import BeautifulSoup
def scrap_ad_data(ad_url):
# request the given url
r = requests.get(ad_url)
data = r.text
# instanciate a new BeatifoulSoup object
soup = BeautifulSoup(data, "html.parser")
#print(soup.prettify())
#target_component = soup.div("ctext2")
target_component = soup.findAll("div", class_="ctext2")
#print(target_component)
#target_component = soup.findAll("search-span", {"class": ["font-normal",
"fs12", "no-margin", "ln22"]})
# create a list that will hold our component data
results = []
for i in target_component:
results.append(''.join(i.text).replace('\n',''))
#results.append(''.join(i.findAll(text=True)).replace('\n',''))
return results
link = "https://www.avito.ma/fr/maroc/voitures-%C3%A0_vendre"
res = scrap_ad_data(link)
print(res)