【发布时间】:2018-04-28 02:02:49
【问题描述】:
我对编程非常陌生,并开始自学使用 Python 进行网络抓取。 我正在从网站的多个页面中抓取玩家数据,并构建了一个 while 循环,该循环抓取“下一个”按钮的 href 以到达下一个玩家的页面。 一切都很好,除了在最后一个可用的玩家之后打破 while 循环。 “下一步”按钮将变灰并且后面没有链接,因此我想停止迭代并将所有内容保存到 csv 中。
我的脚本如下所示:
#name base url and first page to start
BaseUrl = #url
PageUrl = #also url
while True:
#scraping tables
try:
# retrieve link for 'next' player in order
link = soup.find(attrs={"class": "go_to_next_player"}).get('href')
# join base url and new link href
PageUrl = BaseUrl + link
if link is None:
break
except IndexError as e:
print(e)
break
#writing to csv
我以为我可以检查检索到的 href 是否为空,因此检查“is None”并中断,但我收到此错误:
In line > PageUrl = BaseUrl + link
TypeError: must be str, not NoneType
我们将不胜感激!我对此很陌生,所以请忽略我的初学者代码。
【问题讨论】:
标签: python-3.x web-scraping beautifulsoup href nonetype