Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import requests
- from bs4 import BeautifulSoup
- import csv
- # create csv file in folder
- f = csv.writer(open('user-offer.csv', 'w'))
- f.writerow(['Title', 'Location', 'number', 'description', 'pictures'])
- url = 'https://www.olx.bg/ads/user/lpAO/'
- page = requests.get(url)
- # print(page.text)
- if not page.status_code == 200:
- exit()
- html_soup = BeautifulSoup(page.content, "html.parser")
- # print(html_soup.prettify())
- all_user_info = html_soup.find('div', id='listContainer')
- # print(all_user_info)
- user_name = html_soup.find('h3', class_='xxx-large')
- # print(user_name)
- if None in user_name:
- print('You`r not login')
- sys.exit()
- all_user_offers = html_soup.find_all(class_='listHandler')
- # print(len(all_user_offers)) # колко обяви има
- if None in all_user_offers:
- print('No offers')
- for offer in all_user_offers:
- offer_title = offer.find_all('a', class_='marginright5 link linkWithHash detailsLink')
- offer_price = offer.find_all('p', class_='price')
- offer_img = offer.find_all('img', class_='fleft')
- if None in (offer_title, offer_price, offer_img):
- continue
- # print(offer_title)
- # print(offer_price)
- # print(offer_img)
- offer_link_cont = html_soup.find_all('a', attrs={'class': 'marginright5'})
- # print(offer_link_cont)
- urls = []
- for a_tag in offer_link_cont:
- url = a_tag['href']
- urls.append(url)
- # print(urls)
- for user_url_offer in urls:
- url_offer = user_url_offer
- def user_offer_one():
- url = url_offer
- page = requests.get(url)
- # print(page.text)
- if not page.status_code == 200:
- exit()
- html_soup = BeautifulSoup(page.content, "html.parser")
- # print(html_soup.prettify())
- offer_results = html_soup.find('div', id='offerdescription')
- # print(all_offer.prettify())
- offer_title = offer_results.find('div', class_='offer-titlebox').h1
- offer_location = offer_results.find('a', class_='show-map-link').strong
- offer_number = offer_results.find('em').small
- offer_description = offer_results.find('div', id='textContent')
- # print(offer_title.text)
- # print(offer_location.text)
- # print(offer_number.text)
- # print(offer_description.text)
- for offer_img in offer_results:
- offer_imgs = offer_description('div', class_='photo-glow')
- # print(offer_img)
- f.writerow([offer_title, offer_location, offer_number, offer_description, offer_imgs])
- user_offer_one()
- # print(url_offer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement