Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用BeautifulSoup从CSS标记代码中提取信息_Python_Beautifulsoup - Fatal编程技术网

Python 使用BeautifulSoup从CSS标记代码中提取信息

Python 使用BeautifulSoup从CSS标记代码中提取信息,python,beautifulsoup,Python,Beautifulsoup,我正试图从一个有Python的BeautifulSoup库的站点中提取一些信息。我特别想从ccs代码中提取信息: <span class="g47SY ">68</span> 您的代码在查找HTML页面上的元素方面是正确的。问题在于Instagram页面本身。如果您查看它的源代码而不是DevTools元素面板,您会发现它几乎是空白的。Instagram完全使用JavaScript构建,这是一种反模式,但它是一种根深蒂固的模式,因此您要查找的元素只在JavaScript运

我正试图从一个有Python的BeautifulSoup库的站点中提取一些信息。我特别想从ccs代码中提取信息:

<span class="g47SY ">68</span>

您的代码在查找HTML页面上的元素方面是正确的。问题在于Instagram页面本身。如果您查看它的源代码而不是DevTools元素面板,您会发现它几乎是空白的。Instagram完全使用JavaScript构建,这是一种反模式,但它是一种根深蒂固的模式,因此您要查找的元素只在JavaScript运行后存在于客户端中

您可以使用,它基本上在浏览器中打开站点,并执行普通浏览器所能执行的所有操作。例如:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# initialization
driver = webdriver.Firefox()
driver.get("https://www.instagram.com/antedoro/")

try:
    # wait up to 10 seconds for the parent of the spans to be present
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "Y8-fY")))
    # locate the spans
    spans = driver.find_elements_by_css_selectors("span.g47SY")
    text_of_spans = [span.text for span in spans]
finally:
    driver.close()
find_all返回一个列表,因此需要选择第一项。然后使用text属性。像这样:

# I want to extract 68 from <span class="g47SY ">68</span>
info = soup.find_all("span", class_="g47SY")
print(info[0].text)

为什么要投否决票?我刚刚测试过,在bs4中工作

欢迎使用Stackoverflow!为了重现您的问题环境,我可能需要您试图抓取的网站的URL。你能提供吗?斜体粗体代码[span.text代表信息中的span]错误不是因为列表。他正试图通过这样的请求从instagram获取数据。因为汤是空的,所以引起了错误。你需要帮助他,他的问题是如何从HTML68中提取68,这就是我回答的问题。否决一个正确答案是不好的风格。我否决了,因为你给出的修正也会给他带来错误。“它在哪方面是支持性的?”“它不是,我在回答之前测试了它。这是奥普提出的问题。但是,如果你希望成为一个这样的社区,那么你可以选择随机否决正确答案。我很抱歉,因为我假设你没有测试就回答了。我同意你的答案。但是没有办法刮去instagram的网站吗?我添加了一个Selenium的例子。除了Selenium?可能是无头Chrome,但我自己没有用过。没关系,看起来这也是通过Selenium实现的。
# I want to extract 68 from <span class="g47SY ">68</span>
info = soup.find_all("span", class_="g47SY")
print(info[0].text)