def insta_searching(word):
url = "https://www.instagram.com/explore/tags/" + str(word)
return url
def select_first(driver):
first = driver.find_element_by_css_selector("div._9AhH0")[0] # Updated selector for the first post
first.click()
time.sleep(3)
def get_content(driver):
html = driver.page_source
soup = BeautifulSoup(html, 'lxml')
try:
content = soup.select('div.C4VMK > span')[0].text # Updated selector for post content
except:
content = ''
tags = re.findall(r'#[^\s#,\\]+', content)
date = soup.select('time.FH9sR.Nzb55')[0]['datetime'][:10] # Updated selector for post date
try:
place = soup.select('div.M30cS')[0].text # Updated selector for post location
except:
place = ''
data = [content, date, place, tags]
return data
def move_next(driver):
right = driver.find_element_by_css_selector("a._65Bje.coreSpriteRightPaginationArrow") # Updated selector for right arrow
right.click()
time.sleep(3)
driver = webdriver.Chrome(executable_path=r'C:\Users\Smart\Desktop\chromedriver-win64\chromedriver.exe')
driver.get('https://www.instagram.com') # Fixed line
time.sleep(3)
错误:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_11388\2581174010.py in <module>
----> 1 driver = webdriver.Chrome(executable_path=r'C:\Users\Smart\Desktop\chromedriver-win64\chromedriver.exe')
2
3 driver.get('https://www.instagram.com') # Fixed line
4 time.sleep(3)
TypeError: __init__() got an unexpected keyword argument 'executable_path'
这部分不断返回错误,我不知道如何解决这个问题。 我想这可能是因为chrome和
chromedriver
的版本不同,所以我尝试下载相同的版本。但是 chrome 的版本是 116.0.5845.97
并且没有相同版本的 chromedriver
可供我下载。如果您能告诉我解决此错误的方法,那就太好了。请发送帮助
使用硒
v4.10.0
,您需要在executable_path
课程中通过Service
。参考如下:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(executable_path='./chromedriver.exe')
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
话虽如此,如果您不想手动设置
chromedriver.exe
路径,那么您可以简化代码如下:
driver = webdriver.Chrome()
driver.get('https://www.instagram.com')
Selenium 的新工具
Selenium Manager
将为您完成driver.exe
的下载和管理。
参考资料: