在python中使用硒循环csv文件中的链接

时间:2019-07-02 10:59:57

标签: python loops selenium selenium-webdriver web-scraping

我正在尝试打开.csv文件,并使用硒打开.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()

1 个答案:

答案 0 :(得分:0)

  • 首先安装硒:

    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...