我想創建一個字典,將一個類的值存盤為鍵,將另一個類存盤為我正在處理的網頁中的值。
這是我嘗試過的:
from bs4 import BeautifulSoup
import pandas as pd
from selenium import webdriver
DRIVER_PATH = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
all_data = []
for i in range(1, 5, 1):
url = 'https://www.transfermarkt.co.uk/cristiano-ronaldo/profil/spieler/' str(i)
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
data = {}
market_left = soup.find('div', {'class':'right-td'})
market_right = soup.find('div', {'class':'left-td'})
for m in market_left:
for mr in market_right:
print(data[m.text.strip()].append(mr.text.strip()))
但是我收到以下錯誤:
AttributeError: 'NavigableString' 物件沒有屬性 'text'
此外,當我增加范圍內的數字時,例如 from range(1, 10, 1),它似乎沒有迭代許多頁面,它只選擇最后一個。關于如何獲取回圈中每個頁面的資訊的任何想法?
預期輸出:
{'Current market value:':[-, -, -,-,-]}
uj5u.com熱心網友回復:
可以zip在 python 中使用同時迭代兩個串列。
嘗試如下:
for i in range(1,10):
url = "https://www.transfermarkt.co.uk/silvio-adzic/profil/spieler/{}".format(i)
driver.get(url)
time.sleep(5)
soup = BeautifulSoup(driver.page_source, 'html5lib')
market_left = soup.find_all('div',class_="left-td")
market_right = soup.find_all('div',class_="right-td")
print(f"In Page {i}")
for l,r in zip(market_left,market_right):
l_value = l.text.replace('\n','').replace(' ','')
r_value = r.text.replace('\n','').replace(' ','')
print(f"{l_value} {r_value}")
# Code to add the details to dictionary.
print("-----------------------------------------------------------")
很少有頁面包含您正在尋找的資料。
In Page 1
Currentmarketvalue: -
Lastupdate: Apr23,2009
Highestmarketvalue:Lastupdate: £225Th.Oct4,2004
-----------------------------------------------------------
In Page 2
-----------------------------------------------------------
In Page 3
-----------------------------------------------------------
In Page 4
Currentmarketvalue: -
Lastupdate: Feb13,2007
Highestmarketvalue:Lastupdate: £360Th.Oct4,2004
-----------------------------------------------------------
In Page 5
Currentmarketvalue: -
Lastupdate: Sep14,2010
Highestmarketvalue:Lastupdate: £1.26mOct4,2004
-----------------------------------------------------------
In Page 6
Currentmarketvalue: -
Lastupdate: Aug2,2010
Highestmarketvalue:Lastupdate: £765Th.Oct6,2005
-----------------------------------------------------------
In Page 7
Currentmarketvalue: -
Lastupdate: Jan30,2014
Highestmarketvalue:Lastupdate: £1.08mOct4,2004
-----------------------------------------------------------
In Page 8
Currentmarketvalue: -
Lastupdate: Jan2,2010
Highestmarketvalue:Lastupdate: £1.35mJun2,2006
-----------------------------------------------------------
In Page 9
-----------------------------------------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/340884.html
上一篇:Maven AspectJ TestNG Selenium
下一篇:如何將影像更改為結構?用戶界面
