Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.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-从;查阅「;_Python_Beautifulsoup - Fatal编程技术网

Python Beautifulsoup-从;查阅「;

Python Beautifulsoup-从;查阅「;,python,beautifulsoup,Python,Beautifulsoup,我正在尝试使用BeautifulSoup获取一些数据 然而,inspect和view源代码之间似乎存在一些差异(我使用的是chrome,但我不认为这对Pyton是个问题) 这会导致一些问题,因为源代码本身没有显示任何html标记,例如h1。但是,当我使用inspect工具时,它们会出现 我试图刮取的零件(除其他外)-这是使用检查工具显示的: <h1>Teva Pharmaceutical Industries Ltd<small>(TEVA)</small>&

我正在尝试使用BeautifulSoup获取一些数据

然而,inspect和view源代码之间似乎存在一些差异(我使用的是chrome,但我不认为这对Pyton是个问题)

这会导致一些问题,因为源代码本身没有显示任何html标记,例如
h1
。但是,当我使用inspect工具时,它们会出现

我试图刮取的零件(除其他外)-这是使用检查工具显示的:

<h1>Teva Pharmaceutical Industries Ltd<small>(TEVA)</small></h1>
import bs4 as bs
import urllib.request

class Stock:
    stockrow_url = "https://stockrow.com"
    url_suffix = "/financials/{}/annual"


def __init__(self, ticker : str, stock_url=stockrow_url, url_suffix = url_suffix):
    # Stock ticker
    self.ticker = ticker.upper()

    # URLs for financial statements related to the ticker
    self.stock_url = stock_url + "/{}".format(self.ticker)

    sauce = urllib.request.urlopen(self.stock_url).read()
    soup = bs.BeautifulSoup(sauce, 'html.parser').h1
    print(soup)



    self.income_url = self.stock_url + url_suffix.format("income")
    self.balance_sheet_url = self.stock_url + url_suffix.format("balance")
    self.cash_flow_url = self.stock_url + url_suffix.format("cashflow")

teva = Stock("teva")
print(teva.get_income_statement())

页面是使用jscript动态生成的,不能由beautifulsoup处理。您可以使用selenium等工具或通过

在这种情况下,您可以使用

import json
import requests

hdr = {'User-Agent':'Mozilla/5.0'}    
url = "https://stockrow.com/api/companies/TEVA.json?ticker=TEVA"

response = requests.get(url, headers=hdr)
info = json.loads(response.text)
info
同样,损益表也隐藏在这里:

url = 'https://stockrow.com/api/companies/TEVA/financials.json?ticker=TEVA&dimension=MRY&section=Income+Statement'
使用与上面相同的代码,但使用另一个url,将获得json格式的损益表

你可以从那里得到它。四处搜索-有很多关于这个主题的信息。祝你好运