Python Beautifulsoup 访问标签中的文本?

标签 python beautifulsoup

我无法找到并返回 <b> 中出现的值。标签,我在阅读任何标签时都没有运气。

我不想发布一百行查看源信息,并且不确定如何正确发布链接,但如果您能够自己查看页面源,这里是网页 http://yugiohprices.com/card_price?name=Dark+Magician

我试图检索的信息 https://postimg.org/image/5fwxfqjqf/

这是我正在使用的代码

import requests
from bs4 import BeautifulSoup
r = requests.get('http://yugiohprices.com/card_price?name=Dark+Magician'); 
soup = BeautifulSoup(r.content, "lxml")
print soup.find('b').text

这是输出

首页 |前 100 名 |浏览卡片 |浏览集

购买统计 |关注列表 |卡定价器

卖我的卡片|价格提醒|博客 |常见问题 |设置

无论我如何更改或尝试,我都无法访问“LDK2-ENY10”文本

最佳答案

您可以看到页面需要一段时间来加载数据,数据是通过 Ajax 请求请求的,因此请求返回的内容并不是您在浏览器中看到的内容。您可以通过简单的访问http://yugiohprices.com/get_card_prices/Dark+Magician来模拟ajax请求。 ,传递时间戳:

import requests
from time import time

r = requests.get("http://yugiohprices.com/get_card_prices/Dark+Magician?_={}".format(int(time())))

print(r.content)

您将看到有关该卡的所有详细信息,因此要获得您想要的内容,只需找到以 /browse_sets 开头的 href anchor ?设置:

In [1]: import requests
   ...: from time import time
   ...: from bs4 import BeautifulSoup
   ...: 
   ...: r = requests.get("http://yugiohprices.com/get_card_prices/Dark+Magician?
   ...: _={}".format(int(time())))
   ...: soup = BeautifulSoup(r.content, "lxml")
   ...: print(soup.select_one("a[href^=/browse_sets?set]").text)
   ...: 
Legendary Decks II

In [2]: 

关于Python Beautifulsoup 访问标签中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39904089/

相关文章:

python - Pandas:将日期时间对象分配给时间间隔

python - 获取一项的元素查找 FindALL 列表

python - Beautifulsoup 不显示所有 html 元素

python - 使用 BeautifulSoup 通过 Python 从表中获取信息

python - 仅在 Python 中定义用于测试的变量

python - 根据给定的汉明距离折叠字符串集

python - 如何使用 for 循环替换字符串中的多个字符?

python - 无法使用 beautiful soup 获取 HREF

python - 使用 Beautifulsoup 解析 Airdna.co

python - Codeforces 不接受我的输出,即使它看起来完全一样?