【问题标题】:Scraping Yahoo Finance with Python3使用 Python3 抓取雅虎财经
【发布时间】:2023-03-27 12:20:02
【问题描述】:

我是一个完整的抓取新手,我正在尝试抓取https://fr.finance.yahoo.com,但我不知道我做错了什么。

我的目标是抓取索引名称、当前级别和更改(值和百分比)

这是我使用的代码:

import urllib.request

from bs4 import BeautifulSoup


url = 'https://fr.finance.yahoo.com'

request = urllib.request.Request(url)

html = urllib.request.urlopen(request).read()

soup = BeautifulSoup(html,'html.parser')


main_table = soup.find("div",attrs={'data-reactid':'12'})
print(main_table)

links = main_table.find_all("li", class_=' D(ib) Bxz(bb) Bdc($seperatorColor) Mend(16px) BdEnd ')
print(links)

但是,print(links) 显示为空。有人可以帮忙吗?任何帮助将不胜感激,因为我几天来一直在尝试解决这个问题。

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup


    【解决方案1】:

    虽然获取所有字段的更好方法是解析和处理相关的脚本标签,但这是获取所有字段的方法之一。

    import requests
    import pandas as pd 
    from bs4 import BeautifulSoup
    
    url = 'https://fr.finance.yahoo.com/'
    
    r = requests.get(url,headers={"User-Agent":"Mozilla/5.0"})
    soup = BeautifulSoup(r.text,'html.parser')
    
    df = pd.DataFrame(columns=['Index Name','Current Level','Value','Percentage Change'])
    
    for item in soup.select("[id='market-summary'] li"):
        index_name = item.select_one("a").contents[1]
        current_level = ''.join(item.select_one("a > span").text.split())
        value = ''.join(item.select_one("a")['aria-label'].split("ou")[1].split("points")[0].split())
        percentage_change = ''.join(item.select_one("a > span + span").text.split())
        df = df.append({'Index Name':index_name, 'Current Level':current_level,'Value':value,'Percentage Change':percentage_change}, ignore_index=True)
    
    print(df)
    

    输出如下:

            Index Name Current Level     Value Percentage Change
    0           CAC 40       4444,56     -0,88            -0,02%
    1    Euro Stoxx 50       2905,47      0,49            +0,02%
    2        Dow Jones      24438,63    -35,49            -0,15%
    3          EUR/USD        1,0906   -0,0044            -0,40%
    4      Gold future       1734,10     12,20            +0,71%
    5          BTC-EUR       8443,23    161,79            +1,95%
    6   CMC Crypto 200        185,66      4,42            +2,44%
    7      Pétrole WTI         33,28     -0,64            -1,89%
    8              DAX      11073,87      7,94            +0,07%
    9         FTSE 100       5993,28    -21,97            -0,37%
    10         Nasdaq        9315,26     30,38            +0,33%
    11         S&P 500       2951,75      3,24            +0,11%
    12      Nikkei 225      20388,16   -164,15            -0,80%
    13       HANG SENG      22930,14  -1349,89            -5,56%
    14         GBP/USD        1,2177   -0,0051            -0,41%
    

    【讨论】:

    • 非常感谢。它起作用了:-)我不明白的一件事是:“for item in soup.select("[id$='MarketSummary-Proxy'] li"):”“代理”来自哪里?我在 HTML 中找不到它。
    • 好的,我已经更改了该 ID 名称,以便您在检查元素时找到它。谢谢。
    【解决方案2】:

    我认为您需要修复元素选择。

    例如下面的代码:

    import urllib.request
    from bs4 import BeautifulSoup
    url = 'https://fr.finance.yahoo.com'
    
    request = urllib.request.Request(url)
    html = urllib.request.urlopen(request).read()
    
    soup = BeautifulSoup(html,'html.parser')
    
    main_table = soup.find(id="market-summary")
    
    links = main_table.find_all("a")
    for i in links:
        print(i.attrs["aria-label"])
    

    给出具有索引名称、变化百分比、变化和值的输出文本:

    CAC 40 a augmenté de 0,37 % ou 16,55 points pour atteindre 4 461,99 points
    Euro Stoxx 50 a augmenté de 0,28 % ou 8,16 points pour atteindre 2 913,14 points
    Dow Jones a diminué de -0,63 % ou -153,98 points pour atteindre 24 320,14 points
    EUR/USD a diminué de -0,49 % ou -0,0054 points pour atteindre 1,0897 points
    Gold future a augmenté de 0,88 % ou 15,10 points pour atteindre 1 737,00 points
     a augmenté de 1,46 % ou 121,30 points pour atteindre 8 402,74 points
    CMC Crypto 200 a augmenté de 1,60 % ou 2,90 points pour atteindre 184,14 points
    Pétrole WTI a diminué de -3,95 % ou -1,34 points pour atteindre 32,58 points
    DAX a augmenté de 0,29 % ou 32,27 points pour atteindre 11 098,20 points
    FTSE 100 a diminué de -0,39 % ou -23,18 points pour atteindre 5 992,07 points
    Nasdaq  a diminué de -0,30 % ou -28,25 points pour atteindre 9 256,63 points
    S&P 500 a diminué de -0,43 % ou -12,62 points pour atteindre 2 935,89 points
    Nikkei 225 a diminué de -0,80 % ou -164,15 points pour atteindre 20 388,16 points
    HANG SENG a diminué de -5,56 % ou -1 349,89 points pour atteindre 22 930,14 points
    GBP/USD a diminué de -0,34 % ou -0,0041 points pour atteindre 1,2186 points
    

    【讨论】:

      【解决方案3】:

      尝试关注css 选择器以获取所有链接。

      import urllib
      from bs4 import BeautifulSoup
      
      
      url = 'https://fr.finance.yahoo.com'
      request = urllib.request.Request(url)
      html = urllib.request.urlopen(request).read()
      soup = BeautifulSoup(html,'html.parser')
      links=[link['href'] for link in soup.select("ul#market-summary a")]
      print(links)
      

      输出

      ['/quote/^FCHI?p=^FCHI', '/quote/^STOXX50E?p=^STOXX50E', '/quote/^DJI?p=^DJI', '/quote/EURUSD=X?p=EURUSD=X', '/quote/GC=F?p=GC=F', '/quote/BTC-EUR?p=BTC-EUR', '/quote/^CMC200?p=^CMC200', '/quote/CL=F?p=CL=F', '/quote/^GDAXI?p=^GDAXI', '/quote/^FTSE?p=^FTSE', '/quote/^IXIC?p=^IXIC', '/quote/^GSPC?p=^GSPC', '/quote/^N225?p=^N225', '/quote/^HSI?p=^HSI', '/quote/GBPUSD=X?p=GBPUSD=X']
      

      【讨论】:

        猜你喜欢
        • 2020-09-10
        • 2017-01-14
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-21
        相关资源
        最近更新 更多