我想湊網頁用下面的代碼: -循環多個URL
import requests
from bs4 import BeautifulSoup
page = requests.get("http://www.realcommercial.com.au/sold/property-offices-retail-showrooms+bulky+goods-land+development-hotel+leisure-medical+consulting-other-in-vic/list-1?includePropertiesWithin=includesurrounding&activeSort=list-date&autoSuggest=true")
soup = BeautifulSoup(page.content, 'html.parser')
links = soup.find_all('a', attrs ={'class' :'details-panel'})
hrefs = [link['href'] for link in links]
for urls in hrefs:
pages = requests.get(urls)
soup_2 =BeautifulSoup(pages.content, 'html.parser')
Date = soup_2.find_all('li', attrs ={'class' :'sold-date'})
Sold_Date = [Sold_Date.text.strip() for Sold_Date in Date]
Address_1 = soup_2.find_all('p', attrs={'class' :'full-address'})
Address = [Address.text.strip() for Address in Address_1]
上面的代碼只返回從HREF中的第一個網址的細節。
['Mon 05-Jun-17'] ['261 Keilor Road, Essendon, Vic 3040']
我需要遍歷的HREF每個URL運行&從每個URL中的HREF返回類似的細節。 請建議我應該在上面的代碼中添加/編輯什麼。 任何幫助將不勝感激。
感謝
非常感謝Anubhav,它現在對我有用,, –
你能不能也請指導我如何在同一網站上運行相同的代碼說10或20頁,而不必每次都提供每個新頁面的鏈接? –
如果正在工作,請批准答案以結束問題。 –