Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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靓汤的替代品_Python_Pandas_Dataframe_Beautifulsoup_Python Requests - Fatal编程技术网

Python靓汤的替代品

Python靓汤的替代品,python,pandas,dataframe,beautifulsoup,python-requests,Python,Pandas,Dataframe,Beautifulsoup,Python Requests,我写了几行从金融数据网站上获取数据 它只需使用beautiful soup解析和请求获取 有没有其他更简单或更圆滑的方法可以达到同样的效果 我刚讨论完,想看看别人有什么想法 from pandas import DataFrame import bs4 import requests def get_webpage(): symbols = ('ULVR','AZN','HSBC') for ii in symbols: url = 'https://uk.f

我写了几行从金融数据网站上获取数据

它只需使用
beautiful soup
解析和
请求
获取

有没有其他更简单或更圆滑的方法可以达到同样的效果

我刚讨论完,想看看别人有什么想法

from pandas import DataFrame
import bs4
import requests


def get_webpage():
    symbols = ('ULVR','AZN','HSBC')
    for ii in symbols:
        url = 'https://uk.finance.yahoo.com/quote/' + ii + '.L/history?p=' + ii + '.L'
        response = requests.get(url)
        soup = bs4.BeautifulSoup(response.text, 'html.parser')
        rows = soup.find_all('tr')
        data = [[td.getText() for td in rows[i].find_all('td')] for i in range(len(rows))]


        #for i in data:
        # [-7:] Date
        # [-6:] Open
        # [-5:] High
        # [-4:] Low
        # [-3:] Close
        # [-2:] Adj Close
        # [-1:] Volume

        data = DataFrame(data)

        print(ii, data)


if __name__ == "__main__":
    get_webpage()


有什么想法吗?

您可以尝试使用
read\u html()
方法:

symbols = ('ULVR','AZN','HSBC')
df=[pd.read_html('https://uk.finance.yahoo.com/quote/' + ii + '.L/history?p=' + ii + '.L') for ii in symbols]
df1=df[0][0]
df2=df[1][0]
df3=df[2][0]

因为它只是我想要的整个表,所以使用pandas.read_html似乎更容易,尤其是我不需要刮除整个表之外的任何东西

这个网站上有一些有用的信息作为指导

通过使用
导入熊猫作为pd
我得到了我想要的结果

   import pandas as pd

   def get_table()
 
        symbols = ('ULVR','AZN','HSBC')
        position=0
        for ii in symbols:
            table=[pd.read_html('https://uk.finance.yahoo.com/quote/' + ii + '.L/history? 
            p=' + ii + '.L')]

            print (symbols[position])
            print (table, '\n')
        
            position += 1

    if __name__ == "__main__":
        get_table()

你试过yfinance或yahoo finance图书馆吗?不确定它们是否适用于英国金融业,但如果是reglar NYSE,NASDAQ symbolsCheers@JonathanLeon,它们是相当灵活的,我过去曾使用过一点,但它们对于我的膝盖来说似乎有点麻烦。帽子相当整洁@anuragdabas,我为Slicing添加了一个额外的酷部分,尽管这个代码片段可能解决了这个问题,确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。