地址栏显示数据:,同时尝试通过Selenium和Python使用ChromeDriver Chrome进行刮取
我正在做一个简单的刮削程序 首先,用户将写入地址栏显示数据:,同时尝试通过Selenium和Python使用ChromeDriver Chrome进行刮取,python,selenium,google-chrome,selenium-chromedriver,screen-scraping,Python,Selenium,Google Chrome,Selenium Chromedriver,Screen Scraping,我正在做一个简单的刮削程序 首先,用户将写入足球运动员的姓名,然后我将链接到transfermarkt.comweb搜索,然后我想输入第一个链接并从足球运动员的个人资料中刮取数据。 不幸的是,我有硒的问题。如何以编程方式进入网站,并从网站中获取数据 这是我的密码: from urllib.request import urlopen import bs4 from bs4 import BeautifulSoup from selenium import webdriver data = in
足球运动员的姓名
,然后我将链接到transfermarkt.com
web搜索,然后我想输入第一个链接并从足球运动员的个人资料中刮取数据。
不幸的是,我有硒的问题。如何以编程方式进入网站,并从网站中获取数据
这是我的密码:
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)
它只适用于行
打印(搜索)
,但这样我就迷路了。浏览器已打开,但地址栏中只有数据:,
。您只需要无头浏览器:
从selenium导入webdriver
#####
从selenium.webdriver.chrome.options导入选项
选项=选项()
options.add_参数('--headless')
options.add_参数('--disable gpu')
driver=webdriver.Chrome(“/usr/lib/chromium browser/chromedriver”,options=options)
但正如我所说,你不需要在这里使用硒。当您无法使用请求或只想快速编写代码时,请使用selenium
浏览器已打开,但中只有数据:
地址栏
因为您没有在浏览器中获取url:
browser.get(source)
您只需要无头浏览器:
从selenium导入webdriver
#####
从selenium.webdriver.chrome.options导入选项
选项=选项()
options.add_参数('--headless')
options.add_参数('--disable gpu')
driver=webdriver.Chrome(“/usr/lib/chromium browser/chromedriver”,options=options)
但正如我所说,你不需要在这里使用硒。当您无法使用请求或只想快速编写代码时,请使用selenium
浏览器已打开,但中只有数据:
地址栏
因为您没有在浏览器中获取url:
browser.get(source)
看来你很接近。它仅适用于行
print(search)
,因为尽管您已将所需的url构造为search
,但尚未调用传递url的get()
。因此,您需要按如下方式传递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
- 未安装在预期的默认位置
- 正在使用的二进制文件版本之间不兼容
参考文献 有关详细讨论,请参见:
print(search)
,因为尽管您已将所需的url构造为search
,但尚未调用传递url的get()
。因此,您需要按如下方式传递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
- 未安装在预期的默认位置
- 正在使用的二进制文件版本之间不兼容
参考文献 有关详细讨论,请参见:
请求检索url
的内容。get()。text
然后将其传递给BeautifulSoup
?这将消除对selenium的需要。如果您愿意,我可以为您快速制定一个解决方案:)您不需要将BeautifulSoup与selenium结合使用,也不需要在此处使用selenium:)当您无法使用请求或只想快速编写代码时,请使用selenium。您似乎在混合/混淆多种不同的方法/库。我不知道我们现在能做多少。@Cryptoharf84您好,谢谢您的回复。我听说靓汤更适合刮擦,但我听说我不能用它来完成剩下的工作,这就是为什么我决定使用硒。如果没有selenium也能做到这一点,我将非常感谢您的帮助:)@noname说BeautifulSoup比selenium好,或者说BeautifulSoup比selenium好,这没有什么意义,因为它们的用途非常不同,它们不是竞争对手。一个建议是,为什么不使用请求检索url
的内容。get().text
然后将其传递给BeautifulSoup
?这将消除对硒的需求。如果您愿意,我可以为您快速制定一个解决方案:)您不需要将BeautifulSoup与selenium结合使用,也不需要在此处使用selenium:)当您无法使用请求或只想快速编写代码时,请使用selenium。您似乎在混合/混淆多种不同的方法/库。我不知道我们现在能做多少。@Cryptoharf84您好,谢谢您的回复。我听说靓汤更适合刮擦,但我听说我不能用它来完成剩下的工作,这就是为什么我决定使用硒。如果没有硒元素也能做到这一点,我将非常感谢您的帮助:)@noname说BeautifulSoup比硒元素好,或者说BeautifulSoup比硒元素好,这没有什么意义,因为它们的用途非常不同,它们不是竞争对手。谢谢您的解释。也许您知道如何更改这行“驱动程序”。通过“css”选择器(“.spielprofil\u tooltip tooltipstered”)查找“元素”。单击()“当我不使用selenium时?我说的是“或者当您只想快速编写代码时”。因为这比处理请求要多一些。您需要找到该锚点(链接)并获取其href
属性(通过BeautifulSoup)。如果单击此锚点,则此属性是您转到的链接。请重试