如果您访问本网站, https://www.premierleague.com/players, 您将能够看到球员名单以及他们的位置和国籍。 我只需要刮位置,但我做不到
playerss = driver.find_elements(By.XPATH, '//*[@id="mainContent"]/div[2]/div[1]/div/div/table/tbody/tr')
for player in playerss:
position = player.find_element(By.XPATH,'//[@id="mainContent"]/div[2]/div[1]/div/div/table/tbody/tr/td[2]')
print(position)
这是我所拥有的,但它不起作用。如果有人知道如何提供帮助,我将不胜感激。
谢谢!
这就是我获得职位列表的方法:
...
driver.get('https://www.premierleague.com/players')
positions = driver.find_elements(By.CSS_SELECTOR,
'table tbody tr td:nth-child(2)')
for pos in positions:
print(pos.text)
如果你必须使用xpath:
positions = driver.find_elements(By.XPATH, '//table/tbody/tr/td[2]')
球员的位置在:
<td class="hide-s">Midfielder</td>
其中
<td>
的 class 为 hide-s
要打印位置,您需要为presence_of_element_ located()引发WebDriverWait,并且您可以使用以下任一定位器策略:
使用 text 属性:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "table tbody tr td:nth-of-type(2)")))])
使用
innerHTML
:
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "table tbody tr td:nth-of-type(2)")))])
使用
innerText
:
print([my_elem.get_attribute("innerText") for my_elem in WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "table tbody tr td:nth-of-type(2)")))])
控制台输出:
['Midfielder', 'Defender', 'Defender', 'Forward', 'Forward', 'Forward', 'Defender', 'Goalkeeper', 'Forward', 'Defender', 'Defender', 'Defender', 'Defender', 'Defender', 'Midfielder', 'Defender', 'Defender', 'Midfielder', 'Defender', 'Defender', 'Defender', 'Midfielder', 'Goalkeeper', 'Midfielder', 'Midfielder', 'Midfielder', 'Defender', 'Midfielder', 'Forward', 'Forward']