(网页抓取)我已经找到了正确的标签,现在如何提取文本? [英] (Web scraping) I've located the proper tags, now how do I extract the text?

查看:82
本文介绍了(网页抓取)我已经找到了正确的标签,现在如何提取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我的第一个网络抓取应用程序,该应用程序收集 https://store.steampowered.com/.确定方法后,我想对价格重复该过程,然后将二者导出到单独的列中的电子表格中.

I'm creating my first web scraping application that collects the titles of games currently on the "new and trending" tab on https://store.steampowered.com/. Once I figure out how to do this, I want to repeat the process with prices, and export both to a spreadsheet in separate columns.

我已经成功找到了包含我要提取的文本(标题)的标签,但是我不确定一旦找到它们的容器,如何提取标题.

I've successfully found the tags that contain the text I'm trying to extract (the title), but I'm unsure how to extract the titles once I've located their containers.

from urllib.request import urlopen
from bs4 import BeautifulSoup

my_url = 'https://store.steampowered.com/'
uClient = urlopen(my_url)
page_html = uClient.read()
uClient.close()

page_soup = BeautifulSoup(page_html, "html.parser")
containers = page_soup.findAll("div",{"class":"tab_item_name"}, limit=10)

for titles in containers:
    print(titles)

我想做的是使用for循环在垂直列表中打印出现在Steam主页上的10个游戏的名称.实际发生的是我打印出包含标题的标签:

What I'm trying to do is print the names of the 10 games that appear on steam's homepage in a vertical list using a for loop. What actually happens is I print out the tags that contain the titles:

<div class="tab_item_name">Destiny 2: Shadowkeep</div>
<div class="tab_item_name">Destiny 2</div>
<div class="tab_item_name">Destiny 2: Forsaken</div>
<div class="tab_item_name">Destiny 2: Shadowkeep Digital Deluxe Edition</div>
<div class="tab_item_name">NGU IDLE</div>
<div class="tab_item_name">Kaede the Eliminator / Eliminator 小枫</div>
<div class="tab_item_name">Spaceland</div>
<div class="tab_item_name">Cube World</div>
<div class="tab_item_name">Aokana - Four Rhythms Across the Blue</div>
<div class="tab_item_name">CODE VEIN</div>

推荐答案

只需

如果只需要文档或标签的文本部分,则可以使用get_text()方法.它以单个Unicode字符串的形式返回文档中或标签下的所有文本.

If you only want the text part of a document or tag, you can use the get_text() method. It returns all the text in a document or beneath a tag, as a single Unicode string.

所以就这样做:

# Should be `title` IMO, because you are currently handling a single title
for titles in containers:
    print(titles.get_text())

这篇关于(网页抓取)我已经找到了正确的标签,现在如何提取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆