Python Forum
how to seperate words from string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to seperate words from string
#1
hi guys

i am learning scrapping on few different types of sites and i cannot figure it out how to get only 1 word from a string

from bs4 import BeautifulSoup
import requests
import pandas as pd
import re

headers = {
    'Accept': 'application/json',
    'Referer': 'https://www.padmapper.com/apartments/calgary-ab?property-categories=apartment,condo&sort=-price&lease-term=long',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
    'Sec-Fetch-Mode': 'cors',
    'Content-Type': 'application/json',
}

data = '["{\\"sessionId\\":\\"uawg5o3gkk\\",\\"uniqueId\\":\\"2t383bqgq9\\",\\"viewportHeight\\":937,\\"viewportWidth\\":1920,\\"url\\":\\"https://www.padmapper.com/apartments/calgary-ab?property-categories=apartment,condo&sort=-price&lease-term=long\\",\\"site\\":2,\\"timestamp\\":1567001142402,\\"datetime\\":1567001142,\\"abTests\\":{},\\"filters\\":{\\"availableAfter\\":null,\\"availableBefore\\":null,\\"bedrooms\\":[false,false,false,false,false,false],\\"buildingAmenities\\":[],\\"cats\\":null,\\"customMaxPrice\\":false,\\"dogs\\":null,\\"encodedFilters\\":null,\\"excludeAirbnb\\":null,\\"external\\":true,\\"features\\":[],\\"feeds\\":null,\\"floorplans\\":null,\\"hasImages\\":null,\\"isZappable\\":null,\\"keywords\\":null,\\"listingAmenities\\":[],\\"liveByPolygons\\":[],\\"longTerm\\":true,\\"maxLat\\":null,\\"maxLng\\":null,\\"maxDays\\":null,\\"maxPrice\\":5000,\\"maxPricePerBedroom\\":null,\\"maxSquareFeet\\":null,\\"minBathrooms\\":null,\\"minLat\\":null,\\"minLng\\":null,\\"minPrice\\":0,\\"minSquareFeet\\":null,\\"mmFrom\\":null,\\"neighborhoodIds\\":[],\\"noFees\\":null,\\"pad\\":null,\\"polygons\\":[],\\"propertyCategories\\":{\\"apartment\\":true,\\"condo\\":true,\\"house\\":null,\\"room\\":null,\\"other\\":null},\\"q\\":null,\\"qs\\":null,\\"incomeRestricted\\":null,\\"section8\\":null,\\"shortTerm\\":false,\\"sort\\":[\\"-price\\"],\\"sources\\":{\\"apartmentSearch\\":null,\\"airbnb\\":null,\\"homesuite\\":null,\\"forRent\\":null,\\"other\\":null},\\"transits\\":{},\\"url\\":null},\\"location\\":{\\"box\\":{\\"minLng\\":null,\\"maxLng\\":null,\\"minLat\\":null,\\"maxLat\\":null},\\"lat\\":51.0340114880156,\\"lng\\":-114.069383877148,\\"url\\":\\"calgary-ab\\",\\"zoom\\":12,\\"cityName\\":\\"Calgary\\",\\"stateName\\":\\"AB\\",\\"fromMapInteraction\\":false,\\"preventLocationUpdate\\":false},\\"eventAttributes\\":{\\"visiblePropertyIds\\":[\\"l-36076268\\"],\\"loadedPropertyIds\\":[\\"l-36076268\\",\\"l-37665902\\",\\"l-37450465\\",\\"b-914783:p-398495\\",\\"l-36648834\\",\\"b-913871:p-397953\\",\\"b-918153:p-400087\\",\\"b-914844:p-369364\\",\\"l-32346757\\",\\"l-37723485\\",\\"l-37711257\\",\\"b-924031:p-403550\\",\\"l-37735461\\",\\"l-37735462\\",\\"l-26362122\\",\\"l-37711255\\",\\"b-926407:p-404370\\",\\"l-19309641\\",\\"l-24207390\\",\\"l-37628863\\"]},\\"name\\":\\"LIST_ITEM_IMPRESSION\\",\\"versionNumber\\":\\"3.8.0\\"}","{\\"sessionId\\":\\"uawg5o3gkk\\",\\"uniqueId\\":\\"2t383bqgq9\\",\\"viewportHeight\\":937,\\"viewportWidth\\":979,\\"url\\":\\"https://www.padmapper.com/apartments/calgary-ab?property-categories=apartment,condo&sort=-price&lease-term=long\\",\\"site\\":2,\\"timestamp\\":1567001173065,\\"datetime\\":1567001173,\\"abTests\\":{},\\"filters\\":{\\"availableAfter\\":null,\\"availableBefore\\":null,\\"bedrooms\\":[false,false,false,false,false,false],\\"buildingAmenities\\":[],\\"cats\\":null,\\"customMaxPrice\\":false,\\"dogs\\":null,\\"encodedFilters\\":null,\\"excludeAirbnb\\":null,\\"external\\":true,\\"features\\":[],\\"feeds\\":null,\\"floorplans\\":null,\\"hasImages\\":null,\\"isZappable\\":null,\\"keywords\\":null,\\"listingAmenities\\":[],\\"liveByPolygons\\":[],\\"longTerm\\":true,\\"maxLat\\":null,\\"maxLng\\":null,\\"maxDays\\":null,\\"maxPrice\\":5000,\\"maxPricePerBedroom\\":null,\\"maxSquareFeet\\":null,\\"minBathrooms\\":null,\\"minLat\\":null,\\"minLng\\":null,\\"minPrice\\":0,\\"minSquareFeet\\":null,\\"mmFrom\\":null,\\"neighborhoodIds\\":[],\\"noFees\\":null,\\"pad\\":null,\\"polygons\\":[],\\"propertyCategories\\":{\\"apartment\\":true,\\"condo\\":true,\\"house\\":null,\\"room\\":null,\\"other\\":null},\\"q\\":null,\\"qs\\":null,\\"incomeRestricted\\":null,\\"section8\\":null,\\"shortTerm\\":false,\\"sort\\":[\\"-price\\"],\\"sources\\":{\\"apartmentSearch\\":null,\\"airbnb\\":null,\\"homesuite\\":null,\\"forRent\\":null,\\"other\\":null},\\"transits\\":{},\\"url\\":null},\\"location\\":{\\"box\\":{\\"minLng\\":null,\\"maxLng\\":null,\\"minLat\\":null,\\"maxLat\\":null},\\"lat\\":51.0340114880156,\\"lng\\":-114.069383877148,\\"url\\":\\"calgary-ab\\",\\"zoom\\":12,\\"cityName\\":\\"Calgary\\",\\"stateName\\":\\"AB\\",\\"fromMapInteraction\\":false,\\"preventLocationUpdate\\":false},\\"eventAttributes\\":{\\"visiblePropertyIds\\":[\\"l-36076268\\"],\\"loadedPropertyIds\\":[\\"l-36076268\\",\\"l-37665902\\",\\"l-37450465\\",\\"b-914783:p-398495\\",\\"l-36648834\\",\\"b-913871:p-397953\\",\\"b-918153:p-400087\\",\\"b-914844:p-369364\\",\\"l-32346757\\",\\"l-37723485\\",\\"l-37711257\\",\\"b-924031:p-403550\\",\\"l-37735461\\",\\"l-37735462\\",\\"l-26362122\\",\\"l-37711255\\",\\"b-926407:p-404370\\",\\"l-19309641\\",\\"l-24207390\\",\\"l-37628863\\"]},\\"name\\":\\"LIST_ITEM_IMPRESSION\\",\\"versionNumber\\":\\"3.8.0\\"}"]'

r = requests.get('https://www.padmapper.com/apartments/calgary-ab?property-categories=apartment,condo&sort=-price&lease-term=long', headers=headers, data=data)

content = (r.text)
soup = BeautifulSoup(content, 'html.parser')

for section in soup.find_all(class_='ListItemFull_noGutterRow__2UIM- ListItemFull_listItemFull__1k2w4'):
    print(section.find('span', {'class':'ListItemFull_text__1DqPn'}).get_text())
    print(section.find('div', {'class':'ListItemFull_info__3qO0i'}).get_text())
    print(section.find('a', {'class':'ListItemFull_headerText__3OoiC'}).get_text())
    break
returned:

$6,500
3 Bedrooms · 3 Bathrooms Apartment · Downtown Calgary, Calgary
110 7 St SW #1201
basically i would like to get seperate 3 Bedrooms, 3 Bathrooms Apartment  etc..

also i ve got another question:

i am also doing scrapping for different site and there i could use .text function ... and now i had to use get_text().. why is like that?
Reply
#2
input the entire string, strip and split
forexample, change:
print(section.find('span', {'class':'ListItemFull_text__1DqPn'}).get_text())
to
str1 = section.find('span', {'class':'ListItemFull_text__1DqPn'}.text.strip().split()
for word in str1:
    print(word)
# or
print(word[3]) # for the fourth word.
Reply
#3
Thank you very much for you support, it worked out :P
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pulling Specifics Words/Numbers from String bigpapa 2 723 May-01-2023, 07:22 PM
Last Post: bigpapa
  How to seperate dict value? ilknurg 2 1,303 Mar-11-2022, 10:48 AM
Last Post: perfringo
  Extract a string between 2 words from a text file OscarBoots 2 1,826 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,756 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Two separate dataframes, two seperate programs stylingpat 2 1,950 Apr-28-2021, 07:56 PM
Last Post: stylingpat
  Seperate output buffer matt_the_hall 2 2,314 Mar-15-2021, 08:44 PM
Last Post: matt_the_hall
  Replacing a words' letters in a string cananb 2 3,405 Dec-01-2020, 06:33 PM
Last Post: perfringo
  how to check if string contains ALL words from the list? zarize 6 7,036 Jul-22-2020, 07:04 PM
Last Post: zarize
  I want to filter out words with one letter in a string (pig latin translator) po0te 1 2,065 Jan-08-2020, 08:02 AM
Last Post: perfringo
  SQL connection in seperate file? Tommy 1 2,038 Jul-12-2019, 12:42 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020