地址栏显示数据:,同时尝试通过 Selenium 和 Python 使用 ChromeDriver Chrome 进行抓取
Address bar shows data:, while trying to scrape using ChromeDriver Chrome through Selenium and Python
我正在制作一个简单的抓取程序。
首先,用户会写一个footballer
的名字,然后我会做一个link到transfermarkt.com
的网络搜索,然后我想输入第一个link 和 从足球运动员的个人资料中抓取 数据。
不幸的是,我对硒有疑问。如何以编程方式进入网站并从该网站抓取数据?
这是我的代码:
from urllib.request import urlopen
import bs4
from bs4 import BeautifulSoup
from selenium import webdriver
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("/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)
它只适用于第 print(search)
行,但后来我迷路了。浏览器打开了,但是地址栏里只有data:,
你只需要无头浏览器:
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。
Browser is open, but there is only data:, in the
address bar.
因为你没有在浏览器中获取 url:
browser.get(source)
看来你很接近。它只适用于 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
现在,在地址栏中看到文本 data:, 可能有多种原因。错误堆栈跟踪可以帮助我们以更好的方式调试问题。但是,在大多数情况下,此错误是由于以下任一问题造成的:
- google-chrome 未安装在预期的默认位置。
- 您使用的二进制文件版本之间不兼容。
参考
您可以在以下位置找到详细讨论:
我正在制作一个简单的抓取程序。
首先,用户会写一个footballer
的名字,然后我会做一个link到transfermarkt.com
的网络搜索,然后我想输入第一个link 和 从足球运动员的个人资料中抓取 数据。
不幸的是,我对硒有疑问。如何以编程方式进入网站并从该网站抓取数据?
这是我的代码:
from urllib.request import urlopen
import bs4
from bs4 import BeautifulSoup
from selenium import webdriver
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("/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)
它只适用于第 print(search)
行,但后来我迷路了。浏览器打开了,但是地址栏里只有data:,
你只需要无头浏览器:
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。
Browser is open, but there is only data:, in the address bar.
因为你没有在浏览器中获取 url:
browser.get(source)
看来你很接近。它只适用于 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
现在,在地址栏中看到文本 data:, 可能有多种原因。错误堆栈跟踪可以帮助我们以更好的方式调试问题。但是,在大多数情况下,此错误是由于以下任一问题造成的:
- google-chrome 未安装在预期的默认位置。
- 您使用的二进制文件版本之间不兼容。
参考
您可以在以下位置找到详细讨论: