我想凑网页用下面的代码: -循环多个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页,而不必每次都提供每个新页面的链接? –
如果正在工作,请批准答案以结束问题。 –