从网站提取数据:找不到数据

时间:2019-07-20 11:38:43

标签: python python-requests

我试图每分钟从网站提取数据,然后将其写入CSV文件。但是,出现以下错误:

no data found

我需要的是货币名称及其值,如以下示例所示:

     EUR                      0.50

我是Python和数据科学的新手。到目前为止,这是我的代码。它的哪一部分可能导致此错误?

import requests
from bs4 import BeautifulSoup
page = requests.get('https://finviz.com/forex_performance.ashx')
soup = BeautifulSoup(page.content, 'html.parser')
forex = soup.find_all("div", {"class": "content "})
print(forex)

谢谢!

1 个答案:

答案 0 :(得分:0)

您的要求不是很清楚,但是在我看来,在txt文件中写入网页内容的一种简单方法是:

def download(URL):
    import requests
    import bs4

    res=requests.get(URL)
    res.text
    soup=bs4.BeautifulSoup(res.text, 'lxml')
    type(soup)

在这里您可以使用soup.selectsoup.find_all

选择需要打印的内容。

之后,您需要将其写入文件中。

    fin45=open('file.txt', 'w')
    fin45.write(c)
    fin45.close()

c是您从soup.select中提取的。