I am trying to scrape data through a website having many pages with each page having 10 listings & on each listing webpage, there is a hyperlink showing contact number - but only a few initial numbers. The entire number gets visible once i click on that hyperlink. I am unable to figure the way out for including these numbers in my data. Below is my code :
soup_2 =BeautifulSoup(pages.content, 'html.parser')
con_lin=soup_2.find_all('a', attrs ={'href' :'#'})
Contact_Number =[]
for number in con_lin:
Cont = number.text
Contact_Number.append(Cont)
P.S : I am using Python3
Any help/input would be highly appreciated
Thanks
Thanks for the reply, My entire code is :
import requests
from bs4 import BeautifulSoup
urls = []
for i in range(1,5):
pages = "https://www.realcommercial.com.au/sold/in-luddenham%2c+nsw+2745%3bbadgerys+creek%2c+nsw+2555%3bkemps+creek%2c+nsw+2178%3bmount+vernon%2c+nsw+2178%3bcecil+park%2c+nsw+2178%3bgreendale%2c+nsw+2550%3baustral%2c+nsw+2179%3bwallacia%2c+nsw+2745%3berskine+park%2c+nsw+2759%3bbringelly%2c+nsw+2556%3brossmore%2c+nsw+2557/list-{0}?activeSort=date-desc&autoSuggest=true&includePropertiesWithin=includesurrounding&minFloorArea=10000".format(i)
urls.append(pages)
Data = []
for info in urls:
page = requests.get(info)
soup = BeautifulSoup(page.content, 'html.parser')
links = soup.find_all('a', attrs ={'class' :'details-panel'})
hrefs = [link['href'] for link in links]
for href in hrefs:
entry=[]
pages = requests.get(href)
soup_2 =BeautifulSoup(pages.content, 'html.parser')
Address_1 = soup_2.find_all('p', attrs={'class' :'full-address'})
Address = [Address.text.strip() for Address in Address_1]
Date = soup_2.find_all('li', attrs ={'class' :'sold-date'})
Sold_Date = [Sold_Date.text.strip() for Sold_Date in Date]
Area_1 =soup_2.find_all('ul', attrs={'class' :'summaryList'})
Area_2 = [Area.text.strip() for Area in Area_1]
Land_Area = [x for x in Area_2 if x.startswith('Land Area')]
Floor_Area = [y for y in Area_2 if y.startswith('Floor Area')]
Prop_Type = soup_2.find('div', attrs={'class' :'propTypes ellipsis'}).findChildren()
Property_Type=[]
for span in Prop_Type:
Property_Type+=span
Agency_1=soup_2.find_all('div', attrs={'class' :'agencyName ellipsis'})
Agency_Name=[Agency_Name.text.strip() for Agency_Name in Agency_1]
Agent_1=soup_2.find_all('div', attrs={'class' :'agentName ellipsis'})
Agent_Name=[Agent_Name.text.strip() for Agent_Name in Agent_1]
con_lin=soup_2.find_all('a', attrs ={'href' :'#'})
Contact_Number =[]
for number in con_lin:
Cont = number.text
Contact_Number.append(Cont)
entry.append(Address)
entry.append(Sold_Date)
entry.append(Area)
entry.append(Property_Type)
entry.append(Agency_Name)
entry.append(Agent_Name)
entry.append(Contact_Number)
Data.append(entry)
@Andersson : the edit you suggested didn't work. I am getting output as below
[[['Kemps Creek, address available on request'],
['Thu 01-Sep-16'],
['Land Area 10.00ha (24.71 acres) (approx)', 'Floor Area 10,000 m²'],
['Land/Development', 'Commercial Farming'],
['CBRE - Western Sydney'],
['Jason Edge'],
['MyCommercial',
'Previous',
'Next',
'Map',
'0410 6...',
' Save Property',
'Get Email Alerts',
'Real Estate Directory']],
[['320 - 340 Badgerys Creek Road, Badgerys Creek, NSW 2555'],
['Mon 22-Apr-13'],
['Land Area 10.00ha (24.71 acres) (approx)', 'Floor Area 10,000 m²'],
['Land/Development', 'Industrial/Warehouse', 'Retail'],
['CBRE - Western Sydney'],
['Frank Oliveri'],
['MyCommercial',
'Previous',
'Next',
'Map',
'+61 41...',
'Street View',
' Save Property',
'Get Email Alerts',
'Real Estate Directory']],
Contact_Number.append(Cont)line should be included inforloop, otherwise you'll apend only the last value ofCont