我使用Python 3.5,並試圖刮URL列表(同一網站)的列表,代碼如下:刮網址
import urllib.request
from bs4 import BeautifulSoup
url_list = ['URL1',
'URL2','URL3]
def soup():
for url in url_list:
sauce = urllib.request.urlopen(url)
for things in sauce:
soup_maker = BeautifulSoup(things, 'html.parser')
return soup_maker
# Scraping
def getPropNames():
for propName in soup.findAll('div', class_="property-cta"):
for h1 in propName.findAll('h1'):
print(h1.text)
def getPrice():
for price in soup.findAll('p', class_="room-price"):
print(price.text)
def getRoom():
for theRoom in soup.findAll('div', class_="featured-item-inner"):
for h5 in theRoom.findAll('h5'):
print(h5.text)
for soups in soup():
getPropNames()
getPrice()
getRoom()
到目前爲止,如果我打印的湯,讓propNames, getPrice或getRoom他們似乎工作。但我似乎無法通過每個URL並打印getPropNames,getPrice和getRoom。
只有在幾個月的時間裏才學習Python,所以非常感謝您的幫助!
謝謝SebastianOpałczyński,我會把它放在船上,試着讓我的頭靠近它,讓你知道結果! – Maverick