无法在Python中覆盖CSV文件

无法在Python中覆盖CSV文件,python,csv,web-scraping,beautifulsoup,overwrite,Python,Csv,Web Scraping,Beautifulsoup,Overwrite,我已经在这里搜索了答案,在谷歌上花了很长时间,但什么都没有 我已经尝试用“w”而不是“r”或“a”打开文件,但我仍然无法让代码覆盖我最初写入CSV文件的当前结果。我基本上是从网站上抓取信息,我想先搜索一个术语,抓取数据,保存到CSV文件,然后搜索另一个术语,抓取数据,用新数据覆盖当前CSV文件 #!/usr/bin/python3 #from pyvirtualdisplay import Display import csv from bs4 import BeautifulSoup imp

我已经在这里搜索了答案,在谷歌上花了很长时间,但什么都没有

我已经尝试用“w”而不是“r”或“a”打开文件,但我仍然无法让代码覆盖我最初写入CSV文件的当前结果。我基本上是从网站上抓取信息,我想先搜索一个术语,抓取数据,保存到CSV文件,然后搜索另一个术语,抓取数据,用新数据覆盖当前CSV文件

#!/usr/bin/python3
#from pyvirtualdisplay import Display

import csv
from bs4 import BeautifulSoup
import urllib.request


def getPageSource(current_page):

    hdr = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}
    req = urllib.request.Request(current_page, headers=hdr)
    page = urllib.request.urlopen(req)

    soup = BeautifulSoup(page, "html5lib")
    return(soup)


def get_length(file_path):
    with open("data.csv", 'r', encoding='utf8') as csvfile:
        reader = csv.reader(csvfile)
        reader_list = list(reader)
        return len(reader_list)


def write_data(file_path, company_name, role, full_url, date):
    fieldnames = ['ID', 'Company','Role', 'URL', 'Date']
    next_id = get_length(file_path)
    with open(file_path, "w", encoding='utf8') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        #writer.writeheader()
        writer.writerow({
        "ID": next_id,
        "Company": company_name,
        "Role": role,
            "URL": full_url,
                "Date": date
            })
        csvfile.close()



def find_data(source):
    base_url = 'https://www.irishjobs.ie'

    for a in source.find_all(attrs={"itemtype" : "https://schema.org/JobPosting"}):
        job_info = a.find('h2').find('a')
        company_name = a.find('h3').find('a').get_text()
        url = job_info['href']
        full_url = (base_url + url)
        role = (job_info.get_text())
        date = a.find('li',class_='updated-time').get_text().replace('Updated','').strip()
        write_data("data.csv", company_name, role, full_url, date)


if __name__ == '__main__':

    query = input('Enter role to search: ')
    source = getPageSource('https://www.irishjobs.ie/ShowResults.aspx?Keywords='+query+'&Location=102&Category=3&Recruiter=All&SortBy=MostRecent&PerPage=100')

    find_data(source)

您需要将文件保持打开状态,直到完成写入。此外,保持已写入行的计数(使用
enumerate()
)比保持将文件读回更容易:

import csv
from bs4 import BeautifulSoup
import urllib.request


def getPageSource(current_page):

    hdr = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}
    req = urllib.request.Request(current_page, headers=hdr)
    page = urllib.request.urlopen(req)

    return (BeautifulSoup(page, "html5lib"))


def find_data(source):
    base_url = 'https://www.irishjobs.ie'
    fieldnames = ['ID', 'Company','Role', 'URL', 'Date']

    with open('data.csv', 'w', encoding='utf8', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(fieldnames)

        for id, a in enumerate(source.find_all(attrs={"itemtype" : "https://schema.org/JobPosting"}), start=1):
            job_info = a.find('h2').find('a')
            company_name = a.find('h3').find('a').get_text()
            url = job_info['href']
            full_url = (base_url + url)
            role = (job_info.get_text())
            date = a.find('li',class_='updated-time').get_text().replace('Updated','').strip()
            writer.writerow([id, company_name, role, full_url, date])


if __name__ == '__main__':
    query = input('Enter role to search: ')
    source = getPageSource('https://www.irishjobs.ie/ShowResults.aspx?Keywords='+query+'&Location=102&Category=3&Recruiter=All&SortBy=MostRecent&PerPage=100')

    find_data(source)    
这将为您提供
data.csv
启动:

ID、公司、角色、URL、日期
1、计算机期货、Xamarin开发者、,https://www.irishjobs.ie/Jobs/Xamarin-Developer-8143810.aspx,06/03/2018
华莱士·迈尔斯国际,新业务发展经理,https://www.irishjobs.ie/Jobs/New-Business-Development-Manager-8143989.aspx,06/03/2018
3,Requio人力资本有限公司,高级软件开发商-都柏林,https://www.irishjobs.ie/Jobs/Senior-Software-Developer-Dublin-8150128.aspx,20/03/2018

在您的情况下,只使用普通的
csv.writer()
可能比使用
Dictwriter()
更容易。您需要保持文件处于打开状态,直到完成编写。此外,保持已写入行的计数(使用
enumerate()
)比保持将文件读回更容易:

import csv
from bs4 import BeautifulSoup
import urllib.request


def getPageSource(current_page):

    hdr = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}
    req = urllib.request.Request(current_page, headers=hdr)
    page = urllib.request.urlopen(req)

    return (BeautifulSoup(page, "html5lib"))


def find_data(source):
    base_url = 'https://www.irishjobs.ie'
    fieldnames = ['ID', 'Company','Role', 'URL', 'Date']

    with open('data.csv', 'w', encoding='utf8', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(fieldnames)

        for id, a in enumerate(source.find_all(attrs={"itemtype" : "https://schema.org/JobPosting"}), start=1):
            job_info = a.find('h2').find('a')
            company_name = a.find('h3').find('a').get_text()
            url = job_info['href']
            full_url = (base_url + url)
            role = (job_info.get_text())
            date = a.find('li',class_='updated-time').get_text().replace('Updated','').strip()
            writer.writerow([id, company_name, role, full_url, date])


if __name__ == '__main__':
    query = input('Enter role to search: ')
    source = getPageSource('https://www.irishjobs.ie/ShowResults.aspx?Keywords='+query+'&Location=102&Category=3&Recruiter=All&SortBy=MostRecent&PerPage=100')

    find_data(source)    
这将为您提供
data.csv
启动:

ID、公司、角色、URL、日期
1、计算机期货、Xamarin开发者、,https://www.irishjobs.ie/Jobs/Xamarin-Developer-8143810.aspx,06/03/2018
华莱士·迈尔斯国际,新业务发展经理,https://www.irishjobs.ie/Jobs/New-Business-Development-Manager-8143989.aspx,06/03/2018
3,Requio人力资本有限公司,高级软件开发商-都柏林,https://www.irishjobs.ie/Jobs/Senior-Software-Developer-Dublin-8150128.aspx,20/03/2018

在您的情况下,使用普通的
csv.writer()
可能比使用
Dictwriter()

更容易。谢谢您的评论,我是SO的新手,这是来自您这样的长期用户的宝贵信息。我现在要编辑我的问题。最后一件事:您正在使用
“a”
模式。这是显式地要求Python附加到文件中,而不是覆盖它,因此这将不会有用。显示您在“w”模式下尝试的代码(以及它的作用)。还是你想追加?你们的其中一段谈到“向其添加新数据”,另一段谈到“用新数据覆盖”。你想要哪一个?@abarnert,我正在尝试用新数据覆盖文件,而不是附加。我已经更新了我的代码,并将“a”替换为“w”,但我只得到一行一个结果,而不是像我只追加数据时那样得到的100行。我最初包含了所有代码,因为我认为调试会更容易,仅此而已。您正在为每行覆盖_数据。另外,请阅读我之前提供的链接。这对写一个好问题很有帮助。谢谢你的评论,我是SO的新手,这是一个很有价值的信息,来自一个像你这样的长期用户。我现在要编辑我的问题。最后一件事:您正在使用
“a”
模式。这是显式地要求Python附加到文件中,而不是覆盖它,因此这将不会有用。显示您在“w”模式下尝试的代码(以及它的作用)。还是你想追加?你们的其中一段谈到“向其添加新数据”,另一段谈到“用新数据覆盖”。你想要哪一个?@abarnert,我正在尝试用新数据覆盖文件,而不是附加。我已经更新了我的代码,并将“a”替换为“w”,但我只得到一行一个结果,而不是像我只追加数据时那样得到的100行。我最初包含了所有代码,因为我认为调试会更容易,仅此而已。您正在为每行覆盖_数据。另外,请阅读我之前提供的链接。写一个好问题真的很有帮助。非常感谢@Martin Evans。我会采纳你的建议以及其他人的意见,并尝试改进我的问题。我感谢你花时间修改我的代码。干杯。非常感谢你@Martin Evans。我会采纳你的建议以及其他人的意见,并尝试改进我的问题。我感谢你花时间修改我的代码。干杯