【发布时间】:2019-08-16 14:30:09
【问题描述】:
我正在为我的研究设计 scrape 项目,但我坚持在 csv 中写入 scrape 数据。请帮帮我?
我已成功抓取数据,但我想将其存储在 csv 中,下面是我的代码
需要编写代码从网站中提取所有 html,然后将其保存到 csv 文件中。
我认为我需要以某种方式将链接转换为列表,然后编写列表,但我不确定该怎么做。
这是我目前所拥有的:
import requests
import time
from bs4 import BeautifulSoup
import csv
# Collect and parse first page
page = requests.get('https://www.myamcat.com/jobs')
soup = BeautifulSoup(page.content, 'lxml')
print("Wait Scraper is working on ")
time.sleep(10)
if(page.status_code != 200):
print("Error in Scraping check the url")
else:
print("Successfully scrape the data")
time.sleep(10)
print("Loading data in csv")
file = csv.writer(open('dataminer.csv', 'w'))
file.writerow(['ProfileName', 'CompanyName', 'Salary', 'Job', 'Location'])
for pname in soup.find_all(class_="profile-name"):
#print(pname.text)
profname = pname.text
file.writerow([profname, ])
for cname in soup.find_all(class_="company_name"):
print(cname.text)
for salary in soup.find_all(class_="salary"):
print(salary.text)
for lpa in soup.find_all(class_="jobText"):
print(lpa.text)
for loc in soup.find_all(class_="location"):
print(loc.text)
【问题讨论】:
-
首先,使用
.append()将结果保存在列表中。然后保存到csv文件中。参考这个thread -
我是新手,你能告诉我怎么做吗?
标签: python csv web-scraping beautifulsoup