提取标签后如何编辑内容?

时间:2019-10-02 22:41:25

标签: python for-loop web-scraping beautifulsoup

我创建了一个程序,该程序在https://store.steampowered.com/的“新趋势”列下提取游戏名称和游戏价格。到目前为止,我已经将标题和价格垂直打印为单独的列表。

价格打印如下:

$11.99
$9.99
$23.99
$34.99
Free To Play
$24.99
$59.99
Free To Play
$13.49
$19.99

我想做的事情是使用字符串“ Free To Play”以任何价格并将其替换为字符串“ $ 0.00”,因此当我导出到csv时,它看起来更加一致。我已经尝试通过BeautifulSoup使用replace_with()函数,但对我来说却没有用。

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

title_containers = page_soup.findAll("div",{"class":"tab_item_name"}, limit=10)
price_containers = page_soup.findAll("div",{"class":"discount_final_price"}, limit=10)
for titles in title_containers:
    print(titles.get_text())
for prices in price_containers:
    print(prices.get_text())

1 个答案:

答案 0 :(得分:2)

尽管它不使用beautifulsoup,但您可以简单地

...
for prices in price_containers:
    print(prices.get_text().replace('Free To Play', '$0.00'))