我试着重新迭代代码。如果我打印履带名我得到全名,那么下一行什么也不做吗?在这里真的很挣扎。
帮助欣赏我应该做的事
import requests
from bs4 import BeautifulSoup
base_url = "http://www.harness.org.au"
webpage_response = requests.get('http://www.harness.org.au/racing/tracks/', "html.parser")
soup = BeautifulSoup(webpage_response.content, "html.parser")
# only finding one track
# soup.table to find all links for days racing
#harness_table = soup.row
# scraps a href that is an incomplete URL that im trying to get to
tracks = soup.find(class_="col-lg-10 col-md-10 col-sm-10 col-xs-10 content")
lists = []
links = tracks.find_all('a')
#Gets each track
for a in links:
lists.append(base_url+a["href"])
# track1 = []
#purpose - just to get track name before going over other data
for link in lists:
webpage = requests.get(link)
track = BeautifulSoup(webpage.content, "html.parser")
trackname = track.find_all(class_="pageTitle")
track1 = trackname.get_text()
print(trackname)发布于 2020-04-10 09:50:01
当您使用find_all时,它会返回一个元素列表,其中包含您希望它找到的任何标记和属性。因此,您需要从第一个元素中获取文本。或者您可以只使用find,这将返回第一个标记/属性。
所以试着改变一下:
#purpose - just to get track name before going over other data
for link in lists:
webpage = requests.get(link)
track = BeautifulSoup(webpage.content, "html.parser")
trackname = track.find(class_="pageTitle")
try:
track1 = trackname.get_text()
except:
print ('No class="pageTitle" found.')
track1 = ''
print(track1)
No class="pageTitle" found.输出:
Racing
Tracks
Albany
Albion Park
Albury
Ararat
Armidale
Bacchus Marsh
Ballarat
Bankstown
Bathurst
Benalla
...https://stackoverflow.com/questions/61137592
复制相似问题