python-2.7 - from_encoding - bs4



将数据从BeautifulSoup导出到CSV (1)

试试这个脚本并获得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()

[免责声明]我在这个领域已经经历了很多其他的答案,但是他们似乎并不适合我。

我希望能够将我已经抓取的数据导出为CSV文件。

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

当前代码

import requests
from bs4 import BeautifulSoup 

url = "http://implementconsultinggroup.com/career/#/6257"
r = requests.get(url)

req = requests.get(url).text
soup = BeautifulSoup(r.content)
links = soup.find_all("a")

for link in links:
     if "career" in link.get("href") and 'COPENHAGEN' in link.text:
             print "<a href='%s'>%s</a>" %(link.get("href"), link.text)

从代码输出

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()

以CSV格式输出

第1列:网址链接

第2栏:工作描述

例如

第1栏:/职业/管理顾问,以帮助我们的客户成功与它/

第2栏:管理顾问帮助我们的客户取得成功IT COPENHAGEN•在实施顾问集团,我们希望在咨询行业发挥重要作用,因为我们相信,以影响力创造变革的能力是在日益全球化环境中取得成功的先决条件和动荡的世界。





export-to-csv