Advertisement
wansyahi1

Untitled

Jan 15th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import re
  2. import requests
  3. from bs4 import BeautifulSoup
  4.  
  5.  
  6. with open('results.csv', 'w') as f:
  7. f.write("Bussiness Name , Phone Number, Address \n")
  8.  
  9. raw = requests.get('https://www.yellowpages.my/listing/results.php?keyword=boutique&where=selangor&screen=1').text
  10. raw = raw.replace("</br>", "")
  11.  
  12. soup = BeautifulSoup(raw, 'html.parser')
  13.  
  14. name = soup.find_all('div', {'class' :'cbp-vm-companytext'})
  15. # phone = [d.find('a') for d in soup.find_all('div',{'class':'cbp-vm-cta'})]
  16. phone = [re.findall('\>.*?<',d.find('span')['data-content'])[0][1:][:-1] for d in soup.find_all('div',{'class':'cbp-vm-cta'})]
  17. # phone = soup.find_all('span', {'class': 'cbp-vm-cta'})
  18. # phone = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("div", class_='cbp-vm-cta')]
  19. addresses = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("div", class_='cbp-vm-address')]
  20.  
  21. print(phone)
  22.  
  23.  
  24. num_page_items = len(addresses)
  25. with open('results.csv', 'a') as f:
  26. for i in range(num_page_items):
  27. f.write(name[i].text + "," + phone[i] + "," + addresses[i] + "," + "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement