使用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()它返回item的列表。要使用所有单个标签,find()它将返回单个项目。

技巧是获取所有需要的元素的父标记,然后使用您选择和便利的不同方法在这里找到更多。

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')

#scrape table which contains all functions
tabledata = soup.find("table", attrs={"class": "w3-table-all notranslate"})
#print(tabledata)

#from table data get all a tags of functions
functions = tabledata.find_all("a")

#find_all() method returns list of elements iterate over it
for func in functions:
    print(func.contents)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章