我正在抓取这个网站的url,但是当我试图检索所有这些url时,我一直得到错误这是带有url的源页面[![这是带有url的页面源代码][1]][1]
我用这个代码得到了第一个url
soup_bookstore.find('td' , style = "text-align: center;").a.get('href')
这是结果[!][2]][2]
然后尝试使用以下代码检索其余的
book_urls = [x.a.get('href') for x in soup_bookstore.find('td' , style = "text-align: center;")]
# Display number of fetched URLs
print(str(len(book_urls)) + " fetched book URLs")
# We can print all fetched URLS
for book in book_urls:
print(book)
我一直得到这个错误[![3]][3]
我的目标是检索每个课程名称下面的所有url,以放入pandas数据框架中[1]: https://i.stack.imgur.com/TtsUN.png[2]: https://i.stack.imgur.com/pGdYL.png[3]: https://i.stack.imgur.com/8PelX.png
由于您发布的细节不清楚,基于@Vincent的评论
一些td不包含a,所以你不能调用x.a.t get() - Vincent Bitter
book_urls = [x.a.get('href') for x in soup_bookstore.find('td' , style = "text-align: center;") if x.a]
最后的if
条件检查td
是否有子元素a
,并且只有当a
存在时才允许访问href
属性