python - python网页抓取,网页解析器
问题描述
我刚开始学习python,但我在抓取时遇到了问题。代码可以正常工作,但是当我抓取时,却只得到空列表 []。我做错了什么?我找不到同样的问题,感谢您的时间!
`import requests
from bs4 import BeautifulSoup as bs4
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
url = "https://www.worldometers.info/geography/alphabetical-list-of-countries/"
session = requests.session()
try:
req = session.get(url, headers=headers)
if req.status_code == 200:
soup = bs4(req.content, "html.parser")
divs = soup.find_all("div", attrs={"style" : "font-weight"})
name = soup.find_all()
print(divs)
except Exception:
print("ERORR IN URL ADRESS")`
解决方案
您可以获取带有类的表table-condensed
并找到您需要的数据。请检查以下代码:
import requests
from bs4 import BeautifulSoup
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
url = "https://www.worldometers.info/geography/alphabetical-list-of-countries/"
session = requests.session()
try:
req = session.get(url, headers=headers)
if req.status_code == 200:
soup = BeautifulSoup(req.content, "html.parser")
countries = soup.find("table", {"class": "table-condensed"}).find("tbody").findAll("tr")
for country in countries:
print(country.findAll("td")[1].text)
except Exception:
print("ERORR IN URL ADRESS")
推荐阅读
- python - Keras 上的多维 Y_train
- c++ - OSX上的C ++ Boost类型下标运算符错误
- css - IE 11 中不显示前景水印
- azure - Azure DevOps 管道发布 - 错误:getaddrinfo ENOTFOUND
- excel - 如何在 Excel 或 Google 电子表格中计算与前一个日期的价值差异?
- dockerfile - 在 Dockerfile 上使用 ENTRYPOINT 时找不到二进制文件
- c++ - “std::reduce”的正确用法是什么
- regex - 使用正则表达式从文本解析版本的问题
- pandas - pandas/numpy 中的向量与 ndarray
- firebase - Firebase Crashlytics - 警报不会发送到所有者电子邮件