Python / Pycharm无法从终端

时间:2019-06-29 19:53:48

标签: python selenium web-scraping beautifulsoup pycharm

我已经为此困扰了几天,所以任何帮助将不胜感激。我的代码可以在pycharm中正常工作,并且可以完美地写入文件,但是每当我尝试从终端运行文件时,它都不会写入任何CSV文件或其他任何内容,但是代码仍然可以正常运行。

我已向所有应用程序授予了所有必需的权限,例如“写入”和“读取”。

我真的在想我只需要几行代码来帮助将其引导到正确的目录或其他内容?这就是我一直在阅读的内容,但老实说,我对这种类型的东西还很陌生,以至于我不确定执行此操作所需的代码。

这是我的代码,它从Zillow帐户中抓取了联系信息

from selenium import webdriver
import pyperclip
from bs4 import BeautifulSoup
import time
import pandas
from csv import writer
import sys
import os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


#Gets desired location from cmd line args
if len(sys.argv) > 1:
    address = ' '.join(sys.argv[1:])
else:
    address = pyperclip.paste()    

#Chrome webdriver filepath...Chromedriver version 74
driver = webdriver.Chrome(r'C:\Users\mfoytlin\Desktop\chromedriver.exe')
driver.get('https://www.zillow.com/lender-directory/?sort=Relevance&location=' + address)
time.sleep(3)

#Get Page HTML Source
soup = BeautifulSoup(driver.page_source, 'html.parser')
time.sleep(3)

#href_links is really just the profile stuff itself not jsut the links
href_links = soup.findAll('div', {'class': 'ld-lender-card ld-lender-card_bank'})
pages = driver.find_element_by_class_name('zsg-pagination')
num_pages_list = pages.find_elements_by_tag_name('li')
next_button = driver.find_element_by_class_name('zsg-pagination-next')

for page in range(int(num_pages_list[-2].text)):
    print(num_pages_list[-2].text)
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    profile_links = driver.find_elements_by_xpath("//div[@class='ld-lender-info-column']//h2//a")
    with open('posts.csv', 'w') as csv_file:
        csv_writer = writer(csv_file)
        headers = ['Address']
        csv_writer.writerow(headers)
        profile_links = driver.find_elements_by_xpath("//div[@class='ld-lender-info-column']//h2//a")
        for profile in range(len(profile_links)):
            profile_links = driver.find_elements_by_xpath("//div[@class='ld-lender-info-column']//h2//a")
            driver.execute_script("arguments[0].click();", profile_links[profile])
            time.sleep(2)
            soup = BeautifulSoup(driver.page_source, 'html.parser')
            trial = soup.findAll('dl', {'class': 'zsg-list_definition'})
            address = trial[0].dd.text
            csv_writer.writerow([address])
            driver.back()
            time.sleep(2)

    soup = BeautifulSoup(driver.page_source, 'html.parser')
    profile_links = driver.find_elements_by_xpath("//div[@class='ld-lender-info-column']//h2//a")
    href_links = soup.findAll('div', {'class': 'ld-lender-card ld-lender-card_bank'})
    pages = driver.find_element_by_class_name('zsg-pagination')
    num_pages_list = pages.find_elements_by_tag_name('li')
    next_button = driver.find_element_by_class_name('zsg-pagination-next')
    next_button.click()
    time.sleep(2)

0 个答案:

没有答案