【问题标题】:I am web scraping, trying to fetch a set of urls on a website page, however i am getting errors我正在网页抓取,试图在网站页面上获取一组网址,但是我收到错误
【发布时间】:2021-10-07 03:52:18
【问题描述】:

我正在抓取该网站的网址,但是当我尝试检索所有网址时,我不断收到错误消息 这是带有网址的源页面 [![这是带有url的页面的源代码][1]][1]

我得到了这个代码的第一个网址

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

【问题讨论】:

  • 您的帖子基本上无法阅读。删除所有图片并改用文字。
  • 一些td 不包含a,所以你不能调用x.a.get()

标签: python html web-scraping data-science data-mining


【解决方案1】:

由于您发布的细节不清楚,基于@Vincent的评论

有些 td 不包含 a,所以你不能调用 x.a.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 属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多