python-3.x - 使用 Python 3 进行抓取
问题描述
Python3:我不熟悉抓取和训练我正在尝试从此页面获取所有功能:
https://www.w3schools.com/python/python_ref_functions.asp
from bs4 import BeautifulSoup
import requests
url = "https://www.w3schools.com/python/python_ref_functions.asp"
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, 'lxml')
print(soup.td.text)
# Output: abs()
无论我尝试什么,我只得到第一个:abs()
你能帮我把它们全部从 abs() 到 zip() 吗?
解决方案
您可以使用它find_all
来遍历匹配选择器的祖先:
for tag in soup.find_all('td'):
print(tag.text)
这将包括描述列,因此您需要将其更改为忽略单元格。
soup.td 只会返回第一个匹配的标签。
所以一种解决方案是:
for tag in soup.find_all('tr'):
cell = tag.td
if cell:
print(cell.text)
推荐阅读
- next.js - 如何在 Next.js 中处理带有可选 slug 的动态页面的大量重定向?
- xml - python XML或文本声明不在实体的开头
- html - 第一个 HTML 项目 - 损坏的图像
- delphi - 将 OnMouseDown 事件处理程序添加到 VCL 组件?
- sql-server - 使用 TSQL 动态编写数据脚本
- nginx - Nginx 位置块被忽略
- python - 随机森林的手动 k 折交叉验证
- swift - iOS 13 透明工具栏
- python - 为什么 PyTrends/Pandas 程序会抛出错误代码:raise ValueError("No objects to concatenate") ValueError: No objects to concatenate?
- unity3d - 实例化不能与 navmesh 统一工作