程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用 Beautifulsoup 找到一个带有多个文本内容的标签大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用 Beautifulsoup 找到一个带有多个文本内容的标签?

开发过程中遇到如何使用 Beautifulsoup 找到一个带有多个文本内容的标签的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用 Beautifulsoup 找到一个带有多个文本内容的标签的解决方法建议,希望对你解决如何使用 Beautifulsoup 找到一个带有多个文本内容的标签有所启发或帮助;

我正在尝试抓取一个带有多个“p”标签的网站并使用 beautifulsoup,但我发现这非常困难。

我想获取与 p 标签相关联的所有帖子。 Beautifulsoup 上的 find_all 无法完成此操作,图像未保存 我收到一个错误,提示无法保存文件,并告诉我如何检索、添加或刮取中的所有文本p 标签和下面 HTML 代码中的图像。

我的代码

kompas = requests.get('https://url_on_HTML.com/')
beautify = BeautifulSoup(kompas.content,'HTML5lib')

news = beautify.find_all('div',{'class','jeg_block_container'})
arti = []

for each in news:
    Title = each.find('h3','jeg_post_Title'}).text
    lnk = each.a.get('href')
    r = requests.get(lnk)
    soup = BeautifulSoup(r.text,'HTML5lib')
    content = soup.find('p').text.strip()
    images = soup.find_all('img')

    arti.append({
        'headline': Title,'link': lnk,'image': 'images'
        })

让我们将此 HTML 代码作为抓取示例

<HTML><head><Title>The Dormouse's story</Title></head>
<body>
<p class="Title"><b>The Dormouse's story</b></p>

<p>Once upon a time there were three little sisters,and their names were and they lived at the bottom of a well.</p>
<script></script>
<p>the emergency of our matter is Once upon a time there were three little sisters,and their names were and they lived at the bottom of a well.</p>
<p> we will not once in Once upon a time there were three little sisters,and their names were and they lived at the bottom of a well. 
</p>
<script></script>
<br></br>
<script></script>
<p>king of our Once upon a time there were three little sisters,and their names were and they lived at the bottom of a well.</p>
<script></script>
<img src="image.png">
<p>he is our Once upon a time there were three little sisters,and their names were and they lived at the bottom of a well.</p>
<p>some weas Once upon a time there were three little sisters,and their names were and they lived at the bottom of a well.</p>

我想过滤并刮取所有“p”标签并将它们添加到我的内容中。

问题是beautifulsoup 上的find_all 属性无法检索到这个。 查找所有属性只会刮掉 p 元素的第一行。

解决方法

您运行的是 soup.find,而不是 soup.find_allfind_all 将返回所有 p 的列表。您不能在列表上运行 text.strip(),所以让我们将它包装在一个列表推导式中,为所有独立项执行此操作:

soup = BeautifulSoup(r.text,'html5lib')
content = [i.text.strip() for i in soup.find_all('p')]

现在 content 是一个字符串列表。如果你想把这个列表变成单个字符串,你可以运行:

content = ' '.join(content)

关于图像,soup.find_all('img') 还将返回图像列表。要提取链接,您还需要对列表中的所有图像单独执行此操作:images = [i['src'] for i in soup.find_all('img')]

这使得:

kompas = requests.get('https://url_on_html.com/')
beautify = BeautifulSoup(kompas.content,'html5lib')

news = beautify.find_all('div',{'class','jeg_block_container'})
arti = []

for each in news:
    title = each.find('h3','jeg_post_title'}).text
    lnk = each.a.get('href')
    r = requests.get(lnk)
    soup = BeautifulSoup(r.text,'html5lib')
    content = [i.text.strip() for i in soup.find_all('p')]
    content = ' '.join(content)
    images = [i['src'] for i in soup.find_all('img')]

    arti.append({
        'Headline': title,'Link': lnk,'image': images,'content': content
        })

大佬总结

以上是大佬教程为你收集整理的如何使用 Beautifulsoup 找到一个带有多个文本内容的标签全部内容,希望文章能够帮你解决如何使用 Beautifulsoup 找到一个带有多个文本内容的标签所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: