最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

無(wú)法獲取超鏈接href美麗湯

無(wú)法獲取超鏈接href美麗湯

慕尼黑8549860 2023-09-11 16:59:49
我試圖獲取錨點(diǎn) (a) 元素的超鏈接,但我不斷得到:h ttps://in.finance.yahoo.com/h ttps://in.finance.yahoo.com/我已嘗試過(guò)此處提供的所有解決方案:鏈接這是我的代碼:href_links = []symbols = []prices = []commodities = []CommoditiesUrl = "https://in.finance.yahoo.com/commodities"r = requests.get(CommoditiesUrl)data = r.textsoup = BeautifulSoup(data)counter = 40for i in range(40, 404, 14):    for row in soup.find_all('tbody'):        for srow in row.find_all('tr'):            for symbol in srow.find_all('td', attrs={'class':'data-col0'}):                symbols.append(symbol.text)                href_link =  soup.find('a').get('href')                href_links.append('https://in.finance.yahoo.com/' + href_link)            for commodity in srow.find_all('td', attrs={'class':'data-col1'}):                 commodities.append(commodity.text)            for price in srow.find_all('td', attrs={'class':'data-col2'}):                prices.append(price.text)pd.DataFrame({"Links": href_links, "Symbol": symbols, "Commodity": commodities, "Prices": prices })另外,我想知道是否可行,與網(wǎng)站類似,將商品符號(hào)作為我的 pandas 數(shù)據(jù)框中的超鏈接。
查看完整描述

1 回答

?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

我不確定您發(fā)布的代碼發(fā)生了什么,但您可以通過(guò)find將a屬性data-symbol設(shè)置為 的元素簡(jiǎn)單地獲取該 URL GC=F。html 有 2 個(gè)這樣的元素。您想要的是第一個(gè),這是 . 返回的內(nèi)容soup.find('a', {'data-symbol': 'GC=F'}).get('href')。


import requests, urllib


from bs4 import BeautifulSoup


CommoditiesUrl = "https://in.finance.yahoo.com/commodities"

r = requests.get(CommoditiesUrl)

data = r.text

soup = BeautifulSoup(data)


gold_href = soup.find('a', {'data-symbol': 'GC=F'}).get('href')


# If it is a relative URL, we need to transform it into an absolute URL (it always is, fwiw)

if not gold_href.startswith('http'):

? ? # If you insist, you can do 'https://in.finance.yahoo.com" + gold_href

? ? gold_href = urllib.parse.urljoin(CommoditiesUrl, gold_href)


print(gold_url)

另外,我想知道是否可行,與網(wǎng)站類似,將商品符號(hào)作為我的 pandas 數(shù)據(jù)框中的超鏈接。

我對(duì)熊貓不熟悉,但我想說(shuō)答案是肯定的。


查看完整回答
反對(duì) 回復(fù) 2023-09-11
  • 1 回答
  • 0 關(guān)注
  • 130 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)