0
저는 이것이 Python을 처음 접했을 때 매우 간단합니다. 들여 쓰기 문제 일 수 있습니다. 나는 아름다운 스프를 사용하여 여러 웹 페이지를 뒤덮고 데이터를 조작하기 위해 나중에 사용할 수있는 사전 목록을 작성하려고합니다.긁어 모으기 전에 사전 웹 사이트를 호출하여 웹 사이트를 호출합니다.
코드가 제대로 작동하는 것처럼 보이지만, 결국 (liste_flat)로 끝나는 목록은 같은 두 사전의 목록 일뿐입니다. 다른 사전 목록을 원합니다.
def scrap_post(url):
url = "https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDYsImZyZXEiOjUsIm1ldGgiOiJwdWJ0cmFucyIsImxuZyI6LTAuMTI0Nzg5LCJsYXQiOjUxLjUwODR9XQ==/max-time/90/page/".format(i)
dictionary = {}
response = requests.get(url)
soup = bs(response.text,"lxml")
taille = len(soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")) #48 entries
for num_ville in range(0,taille):
print(num_ville)
apt_id = soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")[num_ville]['data-id']
entry = soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")[num_ville]
pricepw = soup.find_all('div', class_='col-xs-5 col-sm-4 price')[num_ville].find('h3').text.encode('utf-8').replace('\xc2\xa3','',).replace('pw','',).strip()
rooms = soup.find_all('div', class_='col-xs-6 type')[num_ville].find('p').text.encode('utf-8').strip()
lat = soup.find_all('div', {"itemprop":"geo"})[num_ville].find('meta', {'itemprop':'latitude'})['content']
lon = soup.find_all('div', {"itemprop":"geo"})[num_ville].find('meta', {'itemprop':'longitude'})['content']
dictionary[num_ville]={'Price per week':pricepw,'Rooms':rooms,'Latitude':lat,'Longitude':lon}
return dictionary
#get all URLs
liste_url = []
liste_url = ['https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDYsImZyZXEiOjUsIm1ldGgiOiJwdWJ0cmFucyIsImxuZyI6LTAuMTI0Nzg5LCJsYXQiOjUxLjUwODR9XQ==/max-time/90/page/''%i' %i for i in range(1,3)]
#get flats
liste_flat = [scrap_post(i) for i in liste_url]
필자는 같은 웹 사이트에서 두 번 반복해야합니다. 다른 웹 사이트를 반복하는지 확인하는 방법에 대한 조언이 있습니까?
감사합니다!
고맙습니다. 정확히 그게 문제였습니다. 추천 한대로 첫 번째 URL을 가져 왔는데 완벽하게 작동합니다. – aliki43