gpt4 book ai didi

python - 如何使用 Beautiful Soup 收集元素?

转载 作者:太空宇宙 更新时间:2023-11-03 20:40:49 25 4
gpt4 key购买 nike

我正在尝试用漂亮的汤制作一个网络抓取工具,但每次我尝试抓取网站时,我都没有得到任何返回。在下面的代码中,我使用请求来获取网站,然后将其放入美丽的 soup 对象中。之后我尝试抓取所有标签。

我尝试观看 YouTube 教程并查看该框架的文档,但我只是不明白如何使用它。

from bs4 import BeautifulSoup
import bs4
import urllib

url = requests.get("https://www.rt.com/")

print(url.status_code)

soup = BeautifulSoup(url.content, 'html.parser')

soup.find_all('div')

最佳答案

您缺少 requests 包,并且您没有对结果执行任何操作。

from bs4 import BeautifulSoup
import requests

url = requests.get("https://www.rt.com/")

print(url.status_code)

soup = BeautifulSoup(url.content, 'html.parser')

divs = soup.find_all('div') # save results to a variable

# Print the text inside each div (example of how to use the results)
for div in divs:
print(div.text)

关于python - 如何使用 Beautiful Soup 收集元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com