-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractlinksv3.py
38 lines (32 loc) · 1.33 KB
/
extractlinksv3.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
import urllib
from bs4 import BeautifulSoup
from nltk.tokenize import word_tokenize, sent_tokenize
import nltk
from nltk.corpus import stopwords
import os
import string
from nltk.text import Text
import sys
orig_stdout = sys.stdout
f = file('/home/amisha/Desktop/sourcedata/listofhotellinks.txt', 'w')
sys.stdout = f
url = 'https://www.tripadvisor.com.sg/Hotels-g294226-Bali-Hotels.html'
for j in range(0,45):
thepage = urllib.urlopen(url).read().decode('utf8')
soup = BeautifulSoup(thepage, "html.parser")
link = soup.find_all(attrs={"class": "cssTruncatedSnippet"})
for i in range(0, len(link)):
soup = BeautifulSoup(urllib.urlopen("http://www.tripadvisor.com" + link[i].find('a')['href']),"html.parser")
checker = link[i].find('a')['href']
urlhotel = "http://www.tripadvisor.com" + link[i].find('a')['href']
print urlhotel
soup = BeautifulSoup(thepage, "html.parser")
linkend = soup.find_all(attrs={"class": "nav next ui_button primary taLnk"})
if len(linkend) == 0:
print len(linkend)
else:
soup = BeautifulSoup(urllib.urlopen("http://www.tripadvisor.com" + linkend[0].get('href')),"html.parser")
Checker = linkend[0].get('href')[-7:]
url = "http://www.tripadvisor.com" + linkend[0].get('href')
sys.stdout = orig_stdout
f.close()