【问题标题】:How to get a link in a dataframe如何在数据框中获取链接
【发布时间】:2020-12-05 03:44:29
【问题描述】:

附上截图我的问题可以很好地解释。

我正在抓取以下页面:https://www.transfermarkt.de/tsg-1899-hoffenheim/kader/verein/533/saison_id/2019/plus/1

表 1 列出了团队。第二列是玩家。我需要您在左下角的屏幕截图中看到的链接。

当我正常查看数据框时,我只在此单元格中得到以下内容:“Oliver BaumannO. BaumannTorwart”但我正在寻找“https://www.transfermarkt.de/oliver-baumann/profil/spieler /55089"。

你们有什么想法吗?

代码:

import pandas as pd
import requests

# Global variables
HEADS = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}
dateiname = 'test.xlsx'
# Global variables

def get_response(url):
   # URL-Anfrage durchfuehren    
   try:
      response = requests.get(url, headers=HEADS)
    
   except AttributeError:
      print('AttributeError') 

   return response


def scraping_kader(response): 
   try:
       dfs = pd.read_html(response.text)
       #dfs = dfs.to_html(escape=False)
       print(dfs[1])
       print(dfs[1].iloc[0, :])

   except ImportError:
       print(' ImportError')
    
   except ValueError:
       print(' ValueError')
    
   except AttributeError:
       print(' AttributeError') 

    
response = get_response('https://www.transfermarkt.de/tsg-1899-hoffenheim/kader/verein/533/saison_id/2019/plus/1')

scraping_kader(response)

【问题讨论】:

  • 如果页面使用 javaScript 生成日期,那么您将需要 Selenium 来控制可以运行 JavaScript 的 Web 浏览器。 requests/pandas/beautifulsoup/lxml 无法运行 JavaScript。
  • 据我所知,pandas 只获取文本表格,您可能需要beautifulsoup/lxml 才能更手动地使用 HTML 并获取链接。

标签: python pandas hyperlink


【解决方案1】:

据我所知read_html 只从表格中获取文本,它不关心链接、隐藏元素、属性等。

您需要像 BeautifulSouplxml 这样的模块来处理完整的 HTML 并手动获取所需的信息。

   soup = BeautifulSoup(response.text, 'html.parser')
   
   all_tooltips = soup.find_all('td', class_='hauptlink')
   
   for item in all_tooltips:
       item = item.find('a', class_='spielprofil_tooltip')
       if item:
           print(item['href']) #, item.text)

此示例仅获取链接,但您可以使用相同的方式获取其他元素。

import requests
from bs4 import BeautifulSoup
#import pandas as pd

HEADS = {
    'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
}

def get_response(url):
   try:
      response = requests.get(url, headers=HEADS)
   except AttributeError:
      print('AttributeError') 

   return response

def scraping_kader(response): 
   try:
       soup = BeautifulSoup(response.text, 'html.parser')
       
       all_tooltips = soup.find_all('td', class_='hauptlink')
       
       for item in all_tooltips:
           item = item.find('a', class_='spielprofil_tooltip')
           if item:
               print(item['href']) #, item.text)
           
       #print(dfs[1])
       #print(dfs[1].iloc[0, :])

   except ImportError:
       print(' ImportError')
    
   except ValueError:
       print(' ValueError')
    
   except AttributeError:
       print(' AttributeError') 

# --- main --

response = get_response('https://www.transfermarkt.de/tsg-1899-hoffenheim/kader/verein/533/saison_id/2019/plus/1')
scraping_kader(response)

结果

/oliver-baumann/profil/spieler/55089
/philipp-pentke/profil/spieler/8246
/luca-philipp/profil/spieler/432671
/stefan-posch/profil/spieler/223974
/kevin-vogt/profil/spieler/84435
/benjamin-hubner/profil/spieler/52348
/kevin-akpoguma/profil/spieler/160241
/kasim-adams/profil/spieler/263801
/ermin-bicakcic/profil/spieler/51676
/havard-nordtveit/profil/spieler/42234
/melayro-bogarde/profil/spieler/476915
/konstantinos-stafylidis/profil/spieler/148967
/pavel-kaderabek/profil/spieler/143798
/joshua-brenet/profil/spieler/207006
/florian-grillitsch/profil/spieler/195736
/diadie-samassekou/profil/spieler/315604
/dennis-geiger/profil/spieler/251309
/ilay-elmkies/profil/spieler/443752
/christoph-baumgartner/profil/spieler/324278
/mijat-gacinovic/profil/spieler/215864
/jacob-bruun-larsen/profil/spieler/293281
/sargis-adamyan/profil/spieler/125614
/felipe-pires/profil/spieler/327911
/robert-skov/profil/spieler/270393
/ihlas-bebou/profil/spieler/237164
/andrej-kramaric/profil/spieler/46580
/ishak-belfodil/profil/spieler/111039
/munas-dabbur/profil/spieler/145866
/klauss/profil/spieler/498862
/maximilian-beier/profil/spieler/578392

【讨论】:

    【解决方案2】:

    这对我有帮助。

    我现在已经用 pandas 复制了该表,并将列替换为您的 BS4 代码中的链接的名称。有效!

    【讨论】:

    • 这不是答案,您应该删除它并通过点击投票按钮旁边的复选标记接受@furas 的答案。
    猜你喜欢
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多