簡體   English   中英

抓取特定運動數據 SELENIUM/BS4

[英]Scraping specific sports data SELENIUM/BS4

我正在嘗試從此頁面https://www.flashscore.pl/druzyna/ajax/8UOvIwnb/tabela抓取數據

Q1:我創建了這段代碼,但我不知道如何為 AJAX 團隊提取數據。 數據將保存為列表。 稍后它們將被保存到 csv 文件中。 另外,我不感興趣,例如符號“?” 如何排除它? 我會很感激你的幫助。

Q2:我如何分離“AJAX”的anserw,例如用“;” Ajax;18;13;3;2;56:4;42;?;W;W;P;W;W;

代碼

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup as BS
import requests
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.flashscore.pl/druzyna/ajax/8UOvIwnb/tabela/")
sleep(10)
page = driver.page_source
soup = BS(page,'html.parser')
content3 = soup.find('div',{'class':'ui-table__body'})
content_list3 = content.find_all('div',{'class':'tableCellFormIcon tableCellFormIcon--TBD'})

for i in content3:
    print(i.text.split()[0])

結果

1.PSV18141346:2443?WWWWR
2.Ajax18133256:442?WWPWW
3.Feyenoord18123342:1739?WPRWW
4.Vitesse18103525:2533?WRWWR
5.Alkmaar18102635:2332?WWWWW
6.Twente1895428:2232?RWWWR
7.Utrecht1885533:2329?RRRPW
8.Cambuur1891832:3928?RPWPW
9.Nijmegen1874724:2625?WWPPP
10.Heerenveen1874720:2525?PWRWR
11.G.A.
12.Groningen1847720:2719?PPRRW
13.Heracles18531021:2618?RWPPP
14.Willem
15.Waalwijk1837819:3016?RPPWR
16.Sparta
17.Sittard18341119:4613?PRWPP
18.Zwolle1813149:326?PPPRR

您可以將其添加到列表中:

res = []
for i in content3:
    line = i.text.split()[0]
    print(line)
    res.append(line)

https://docs.python.org/3/tutorial/datastructures.html -

list.append(x) 將一個項目添加到列表的末尾。 等價於 a[len(a):] = [x]。


替換“?” 添加這個:

line = line.replace("?", "")

https://docs.python.org/3/library/stdtypes.html#str.replace -

str.replace(old, new[, count]) 返回字符串的副本,其中所有出現的 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 舊替換為新。 如果給定了可選參數 count,則僅替換第一個 count 出現。

Added regular expressions and sorted "Ajax"
import re 
...
res = []
for i in content3:
    line = i.text.split()[0]
    if re.search('Ajax', line):
        line = line.replace("?", "")
        res.append(line)

print(res)

主題的另一個問題。 我怎樣才能用單獨的“;”得到結果

結果

 ['1.Ajax20153261:548WWWWP']

預期結果(單獨的;在本例中缺少幾行值 20 和值 48)

Ajax;15;3;2;61:5;W;W;W;W;P'

下面的代碼

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup as BS
import requests
from time import sleep
import re
driver = webdriver.Chrome()
driver.get("https://www.flashscore.pl/druzyna/ajax/8UOvIwnb/tabela/")
sleep(10)
page = driver.page_source
soup = BS(page,'html.parser')
content3 = soup.find('div',{'class':'ui-table__body'})
content_list3 = content3.find_all('div',{'class':'tableCellFormIcon 
tableCellFormIcon--TBD'})
res = []
for i in content3:
   line = i.text.split()[0]
   if re.search('Ajax', line):
       line = line.replace("?", "")
       res.append(line)

print(res)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM