2
使用findAll
會給出錯誤「TypeError:list indices must be integers,not str」,其中使用.find
沒有。使用findall
會給出錯誤「TypeError:'NoneType'對象不可調用」。Python - 如何獲取所有實例而不僅僅是頁面上的第一個
什麼是正確的方式來定位頁面上有一類「框架」的所有鏈接,而不是隻是第一個實例?
import requests
from bs4 import BeautifulSoup
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/page/2/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/page/3/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/page/4/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/page/5/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/page/6/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/page/7/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/page/8/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]
url = ("http://www.gym-directory.com/listing-category/gyms-fitness-centres/page/9/")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
print soup.findAll("a",{"class":"frame"})["href"]