我在從串列中選擇合適的專案時遇到問題。
例如 - 我想省略“1”。然后是第一個“5”(如示例中)另外,我想寫一個條件,字母“W”應更改為“WIN”。
import re
from selenium import webdriver
from bs4 import BeautifulSoup as BS2
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.flashscore.pl/druzyna/ajax/8UOvIwnb/tabela/")
sleep(10)
page = driver.page_source
soup = BS2(page,'html.parser')
content = soup.find('div',{'class':'ui-table__body'})
content_list = content.find_all('span',{"table__cell table__cell--value"})
res = []
for i in content:
line = i.text.split()[0]
if re.search('Ajax', line):
res.append(line)
print(res)
結果
['1.Ajax550016:315?WWWWW']
我需要
Ajax;5;5;0;16;3;W;W;W;W;W
uj5u.com熱心網友回復:
我建議您選擇更具體的元素:
for e in soup.select('.ui-table__row'):
迭代ResultSet和decompose()不需要的標簽:
e.select_one('.wld--tbd').decompose()
提取文本stripped_strings和join()它們到您預期的字串:
data.append(';'.join(e.stripped_strings))
例子
還做了一些替換,dict只是為了演示這將如何作業,不知道R或P.
...
soup = BS2(page,'html.parser')
data = []
for e in soup.select('.ui-table__row'):
e.select_one('.wld--tbd').decompose()
e.select_one('.tableCellRank').decompose()
e.select_one('.table__cell--points').decompose()
e.select_one('.table__cell--score').string = ';'.join(e.select_one('.table__cell--score').text.split(':'))
pattern = {'W':'WIN','R':'RRR','P':'PPP'}
data.append(';'.join([pattern.get(i,i) for i in e.stripped_strings]))
data
僅獲得Ajax的結果:
data = []
for e in soup.select('.ui-table__row:-soup-contains("Ajax")'):
e.select_one('.wld--tbd').decompose()
e.select_one('.tableCellRank').decompose()
e.select_one('.table__cell--points').decompose()
e.select_one('.table__cell--score').string = ';'.join(e.select_one('.table__cell--score').text.split(':'))
pattern = {'W':'WIN','R':'RRR','P':'PPP'}
data.append(';'.join([pattern.get(i,i) for i in e.stripped_strings]))
data
輸出
根據實際資料,它可能與問題示例不同。
['Ajax;6;6;0;0;21;3;WIN;WIN;WIN;WIN;WIN']
uj5u.com熱心網友回復:
您通過使用 bs4 找到表格 div 是正確的開始,但后來您放棄了,只是嘗試使用 re 從文本中提取。如您所見,這是行不通的。這是破解并獲得您想要的東西的簡單方法。我從你找到的表格 div 中保持抓取 div,并在找到 Ajax 后抓取接下來的八個 div 的文本。然后我做了一些骯臟的字串操作,因為 WWWWW 都在同一個頂級 div 中。
import re
from selenium import webdriver
from bs4 import BeautifulSoup as BS2
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
#driver = webdriver.Chrome()
driver.get("https://www.flashscore.pl/druzyna/ajax/8UOvIwnb/tabela/")
driver.implicitly_wait(10)
page = driver.page_source
soup = BS2(page,'html.parser')
content = soup.find('div',{'class':'ui-table__body'})
content_list = content.find_all('span',{"table__cell table__cell--value"})
res = []
found = 0
for i in content.find('div'):
line = i.text.split()[0]
if re.search('Ajax', line):
found = 8
if found:
found -= 1
res.append(line)
# change field 5 into separate values and skip field 6
res = res[:4] res[5].split(':') res[7:]
# break the last field into separate values and drop the first '?'
res = res[:-1] [ i for i in res[-1]][1:]
print(";".join(res))
回傳
Ajax;5;5;0;16;3;W;W;W;W;W
這可行,但它非常脆弱,并且一旦網站更改其內容就會中斷。你應該進行大量的錯誤檢查。我還用等待呼叫替換了 sleep,并添加了 chromedrivermamager,它允許我將 selenium 與 chrome 一起使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507734.html
