首页 > 解决方案 > 在python中使用硒循环csv文件中的链接

问题描述

我正在尝试打开一个 .csv 文件,并使用 selenium 打开 .csv 文件中的链接,并循环浏览 .csv 文件中的链接。我是 Selenium 的新手。我可以很容易地用漂亮的汤做到这一点。请你指导我正确的方向。

from selenium import webdriver
from bs4 import BeautifulSoup as bs
import csv
import requests

contents =[]

filename = 'link_business_filter.csv'

def copy_json():
    with open('vendors_info_bangkok.json',"a") as wt:
        for x in script3:
            wt.write(x)
            wt.close()
            return

with open(filename,'rt') as f:
    data = csv.reader(f)
    for row in data:
        links = row[0]
        contents.append(links)

for link in contents:
    url_html = requests.get(link)

    browser = webdriver.Chrome('chromedriver')
    for link_loop in url_html:

       open = browser.get(link_loop)

       source = browser.page_source
       data = bs(source,"html.parser")
       body = data.find('body')
       script = body
       x_path = '//*[@id="react-root"]/section/main/div'
       script2 = browser.find_element_by_xpath(x_path)
       script3 = script2.text
       print(script3)
       copy_json()

标签: pythonloopsseleniumselenium-webdriverweb-scraping

解决方案


  • 首先安装硒:

    pip install selenium
    
  • 然后根据您的os安装chromediver,然后通过转到保存驱动程序的文件夹并打开终端并输入来测试它chromedriver,如果没有错误则它可以工作。

  • 然后在您的代码中,您需要executable_path提供chromdriver

在你的代码中:

....code...


for link in contents:
    url_html = requests.get(link)

    path to chromdriver = 'C:/Users/chromedriver.exe'    #<-- you can keep this file anywhere you wish

    browser = webdriver.Chrome(executable_path= 'path_to_chromdriver')    #<-- you can also give the path directly here
    for link_loop in url_html:


    ...code...

推荐阅读