首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只想从Href收到短信

只想从Href收到短信
EN

Stack Overflow用户
提问于 2020-04-10 09:38:16
回答 1查看 51关注 0票数 0

我试着重新迭代代码。如果我打印履带名我得到全名,那么下一行什么也不做吗?在这里真的很挣扎。

帮助欣赏我应该做的事

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-10 09:50:01

当您使用find_all时,它会返回一个元素列表,其中包含您希望它找到的任何标记和属性。因此,您需要从第一个元素中获取文本。或者您可以只使用find,这将返回第一个标记/属性。

所以试着改变一下:

代码语言:javascript
复制
#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.

输出:

代码语言:javascript
复制
Racing
Tracks
Albany
Albion Park
Albury
Ararat
Armidale
Bacchus Marsh
Ballarat
Bankstown
Bathurst
Benalla
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61137592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档