-
Notifications
You must be signed in to change notification settings - Fork 0
/
blablacar.py
59 lines (48 loc) · 1.45 KB
/
blablacar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import bs4 as BeautifulSoup
import re
from urllib.parse import quote
from httprequests import *
##
# Blablacar link
# origin
# destination
# date
##
def get_blablacar_url(origin, destination, date, limit):
origin = quote(origin)
destination = quote(destination)
return u"https://www.blablacar.co.uk/search?db=" + date + "&fn=" + origin + "&tn=" + destination + "&sort=trip_date&order=asc&limit=" + limit + "&page=1"
##
#
##
def get_blablacar_soup(url):
html = https_request(url)
save_html(html, "blablacar")
return BeautifulSoup.BeautifulSoup(html, "lxml")
##
#
##
def get_number_blablacar(soup):
text = soup.find('div', 'pagination-info span3').get_text(strip=True)
results = re.search('.+ ([0-9]+) results', text)
if results:
number = results.group(1)
return number
def blablacar_search(origin, destination, date):
number = "100"
url = get_blablacar_url(origin, destination, date, number)
soup = get_blablacar_soup(url)
number = get_number_blablacar(soup)
if int(number) > 100:
url = get_blablacar_url(origin, destination, date, number)
print(url)
soup = get_blablacar_soup(url)
for p in soup.find_all('h2', 'username'):
print(p.encode('utf-8').decode('cp1252'))
elif int(number) > 0:
print(url)
for p in soup.find_all('h2', 'username'):
print(p.encode('utf-8').decode('cp1252'))
else:
print("no results!")
pass