亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

同時嘗試通過 Selenium 和 Python 使用 ChromeDriver Chrom

同時嘗試通過 Selenium 和 Python 使用 ChromeDriver Chrom

慕哥9229398 2022-07-19 20:30:42
我正在制作一個簡單的抓取程序。首先,用戶會寫下 a 的名字footballer,然后我會創建一個鏈接到transfermarkt.com網絡搜索,然后我想輸入第一個鏈接并從足球運動員的個人資料中抓取數據。不幸的是,我對硒有疑問。如何以編程方式進入網站并從網站上抓取數據?這是我的代碼:from urllib.request import urlopenimport bs4from bs4 import BeautifulSoupfrom selenium import webdriverdata = input('Enter name: ')data = data.replace(" ", "+")print(data)link = 'https://www.transfermarkt.pl/schnellsuche/ergebnis/schnellsuche?query='search = link + data + '&x=0&y=0'print(search)driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")driver.find_element_by_css_selector('.spielprofil_tooltip tooltipstered').click()name_box = soup.find('h1', attrs={'class': 'dataValue'})print(name_box)它只適用于 line print(search),但后來我迷路了。瀏覽器是打開的,但只有data:,在地址欄中。
查看完整描述

2 回答

?
慕田峪9158850

TA貢獻1794條經驗 獲得超8個贊

您只需要無頭瀏覽器:


from selenium import webdriver

#####

from selenium.webdriver.chrome.options import Options


options = Options()

options.add_argument('--headless')

options.add_argument('--disable-gpu') 

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", options=options)

但正如我所說,你不需要在這里使用硒。當您無法使用請求或只想快速編寫代碼時,請使用 selenium。


瀏覽器已打開,但地址欄中只有 data:,。


因為您沒有在瀏覽器中獲取 url:


browser.get(source)


查看完整回答
反對 回復 2022-07-19
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

看來你很接近了。它僅適用于該行print(search),因為盡管您已經構建了所需的 url,因為search您還沒有調用get()傳遞 url。因此,您需要按如下方式傳遞 url:


代碼塊:


options = webdriver.ChromeOptions() 

options.add_argument("start-maximized")

data = input('Enter name: ')

data = data.replace(" ", "+")

print(data)

link = 'https://www.transfermarkt.pl/schnellsuche/ergebnis/schnellsuche?query='

search = link + data + '&x=0&y=0'

print(search)

driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')

driver.get(search)

控制臺輸出:


Enter name: Kylian Mbappé

Kylian+Mbappé

https://www.transfermarkt.pl/schnellsuche/ergebnis/schnellsuche?query=Kylian+Mbappé&x=0&y=0

現在看到文本數據背后可能有多種原因:在地址欄中。錯誤堆棧跟蹤將幫助我們以更好的方式調試問題。但是,在大多數情況下,此錯誤是由以下任一問題引起的:


谷歌瀏覽器未安裝在預期的默認位置。

您使用的二進制文件版本之間不兼容。


查看完整回答
反對 回復 2022-07-19
  • 2 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號