【发布时间】:2019-12-07 15:16:20
【问题描述】:
我正在尝试使用以下代码计算网页上的链接数:
import requests
from requests.exceptions import HTTPError
from bs4 import BeautifulSoup
import pandas as pd
webpage = "https://www.isode.com/products/index.html"
try:
response = requests.get(webpage)
#response.raise_for_status()
except HTTPError:
print("A HTTP Error has occured")
except Exception as err:
print(err)
else:
print("The request of the webpage was a success!")
contents = response.content
contents
soup = BeautifulSoup(contents, features = "html.parser")
a = 0
for link in soup.find_all("a"):
if link.get("href"):
a=a+1
print(link.get("href")
我的预期答案是 86,但这段代码给了我 83,所以我不知道哪里出错了?
此外,就拥有一个计数变量而言 - 肯定有更好的方法来做到这一点吗?
【问题讨论】:
-
为什么你的期望值是 86?我只是在页面源上数了一下,正文中有83个href
-
这就是练习的解决方案——所以不只是我疯了!
-
喜欢上学锻炼?我会问你的教授,自从他们检查之后,页面是否发生了变化。
-
@user1558604 实际上我只数了 86 - 我想你忘记了“市场”下拉菜单中的子链接
-
@user1558604 啊,那是因为我只在寻找href链接,而我想要所有的链接!
标签: python beautifulsoup request