Chrome 网络抓取Instagram时出现问题

hgb9j2n6  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(103)
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可供我下载。如果你能告诉我一个修复这个错误的方法,那就太好了。请发送帮助

6l7fqoea

6l7fqoea1#

对于selenium v4.10.0,您需要在Service类中传递executable_path。参考如下:

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
参考文献:

相关问题