Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 将数据从BeautifulSoup导出到CSV_Python 2.7_Parsing_Web Scraping_Beautifulsoup_Export To Csv - Fatal编程技术网

Python 2.7 将数据从BeautifulSoup导出到CSV

Python 2.7 将数据从BeautifulSoup导出到CSV,python-2.7,parsing,web-scraping,beautifulsoup,export-to-csv,Python 2.7,Parsing,Web Scraping,Beautifulsoup,Export To Csv,[免责声明]我已经阅读了该领域的许多其他答案,但它们似乎对我不起作用 我希望能够将我刮取的数据导出为CSV文件 我的问题是如何编写将数据输出到CSV的代码 现行代码 以CSV格式输出 第1列:Url链接 第2栏:工作说明 例如 第1栏:/职业/管理顾问,帮助我们的客户在以下方面取得成功:- 它/ 专栏2:管理顾问帮助我们的客户在IT方面取得成功 哥本哈根•在实施咨询集团,我们希望在 咨询业,因为我们相信创造变化的能力 具有影响力是在日益全球化和全球化的环境中取得成功的先决条件 动荡的世界。尝试此

[免责声明]我已经阅读了该领域的许多其他答案,但它们似乎对我不起作用

我希望能够将我刮取的数据导出为CSV文件

我的问题是如何编写将数据输出到CSV的代码

现行代码

以CSV格式输出

第1列:Url链接

第2栏:工作说明

例如

第1栏:/职业/管理顾问,帮助我们的客户在以下方面取得成功:- 它/

专栏2:管理顾问帮助我们的客户在IT方面取得成功 哥本哈根•在实施咨询集团,我们希望在 咨询业,因为我们相信创造变化的能力 具有影响力是在日益全球化和全球化的环境中取得成功的先决条件
动荡的世界。

尝试此脚本并获得csv输出:

import csv ; import requests
from bs4 import BeautifulSoup 

outfile = open('career.csv','w', newline='')
writer = csv.writer(outfile)
writer.writerow(["job_link", "job_desc"])

res = requests.get("http://implementconsultinggroup.com/career/#/6257").text
soup = BeautifulSoup(res,"lxml")
links = soup.find_all("a")

for link in links:
     if "career" in link.get("href") and 'COPENHAGEN' in link.text:
        item_link = link.get("href").strip()
        item_text = link.text.replace("View Position","").strip()
        writer.writerow([item_link, item_text])
        print(item_link, item_text)
outfile.close()

您必须将结果存储在列表中。谢谢Adam。我是Python新手,您能快速演示如何创建/存储结果作为一个列表吗?下面是我对一个类似问题的回答:所以我只需要在这篇文章中添加内容?tables=soup.find_all'table'数据=[]对于表中的表:previous=table.find_previous_同胞'h2'id=previous[0]。如果previous else无行,则获取'id'=[td.get_textstrip=True,对于表中的td.find_all'td']数据。追加[id]+rowsOr您编写的代码的哪些部分与我的情况相关?谢谢Shahin-这正是我想要的。唯一不起作用的功能是最后一部分:outfile.close:File,第7行outfile.close^SyntaxError:invalid syntaxer这是因为我想,您使用的是python 2,而我使用的是python 3。不过我不确定!然而,在我看来,它运行得完美无缺。
View Position

</a>
<a href='/career/management-consultants-to-help-our-customers-succeed-with-
it/'>
Management consultants to help our customers succeed with IT
COPENHAGEN • At Implement Consulting Group, we wish to make a difference in 
the consulting industry, because we believe that the ability to create Change 
with Impact is a precondition for success in an increasingly global and 
turbulent world.




View Position

</a>
<a href='/career/management-consultants-within-process-improvement/'>
Management consultants within process improvement
COPENHAGEN • We are looking for consultants with profound
experience in Six Sigma, Lean and operational
management
with open('ImplementTest1.csv',"w") as csv_file:
     writer = csv.writer(csv_file)
     writer.writerow(["link.get", "link.text"])
     csv_file.close()
import csv ; import requests
from bs4 import BeautifulSoup 

outfile = open('career.csv','w', newline='')
writer = csv.writer(outfile)
writer.writerow(["job_link", "job_desc"])

res = requests.get("http://implementconsultinggroup.com/career/#/6257").text
soup = BeautifulSoup(res,"lxml")
links = soup.find_all("a")

for link in links:
     if "career" in link.get("href") and 'COPENHAGEN' in link.text:
        item_link = link.get("href").strip()
        item_text = link.text.replace("View Position","").strip()
        writer.writerow([item_link, item_text])
        print(item_link, item_text)
outfile.close()