多个for循环和csv文件

问题描述 投票:0回答:2

我是新来的和python的新手,目前正在学习一些基本的东西,大多是刮,我遇到了一个问题,我希望你能帮我解决。

我正试图从网站上抓取一些细节并将它们写入CSV文件,但我只能将最后的结果写入我的CSV,显然我的脚本只是覆盖了数据。

此外,如果您发现我的代码有任何错误或有任何改进空间(我确信有),我会很高兴你也会指出它们。

另外,任何可以帮助我提高我的python和抓取技能的视频/教程的建议将不胜感激。

import requests
from bs4 import BeautifulSoup
import csv

url = 'https://www.tamarackgc.com/club-contacts'
source = requests.get(url).text
soup = BeautifulSoup (source, 'lxml')

csv_file = open('contacts.csv', 'w')
csv_writer = csv.writer (csv_file)
csv_writer.writerow(["department", "name", "position", "phone"])

for department in soup.find_all("div", class_="view-content"):
    department_name = department.h3
    print (department_name.text)

for contacts in soup.find_all("div", class_="col-md-7 col-xs-10"):
    contact_name = contacts.strong
    print(contact_name.text)

for position in soup.find_all("div", class_="field-content"):
    print(position.text)

for phone in soup.find_all("div", class_="modal-content"):
    first_phone = phone.h3
    first_phones = first_phone
    print(first_phones)

csv_writer.writerow([department_name, contact_name, position, first_phones])

csv_file.close()
python python-2.7 web-scraping beautifulsoup
2个回答
2
投票

谢谢托马斯,实际上我通过思考如何让它变得更简单来调整我的代码(四个循环太多,不是吗?)所以使用下面的代码我解决了我的问题(放弃了'部门'和'手机',因为其他一些问题):

import requests
from bs4 import BeautifulSoup
import csv

url = 'https://www.tamarackgc.com/club-contacts'
source = requests.get(url).text
soup = BeautifulSoup (source, 'lxml')



f = open("contactslot.csv", "w+")

csv_writer = csv.writer (f)
csv_writer.writerow(["Name", "Position"])

infomation = soup.find_all("div", class_="well profile")
info = information[0]

for info in information:
    contact_name = info.find_all("div", class_="col-md-7 col-xs-10")
    names = contact_name[0].strong
    name = names.text
    print (name)


    position_name = info.find_all("div", class_="field-content")
    position = position_name[0].text
    print(position)
    print("")
    csv_writer.writerow([name, position])

f.close()

1
投票

嗨Babr欢迎使用python。你的答案很好,这是你可以做得更好的一件小事。

如果你只想要一个元素,请使用find替换find_all

import requests
from bs4 import BeautifulSoup
import csv

url = 'https://www.tamarackgc.com/club-contacts'
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')

f = open("/Users/mingjunliu/Downloads/contacts.csv", "w+")

csv_writer = csv.writer(f)
csv_writer.writerow(["Name", "Position"])

for info in soup.find_all("div", class_="well profile"):
    contact_name = info.find("div", class_="col-md-7 col-xs-10")
    names = contact_name.strong
    name = names.text
    print(name)

    position_name = info.find("div", class_="field-content")
    position = position_name.text
    print(position)
    print("")
    csv_writer.writerow([name, position])

f.close()

你需要放弃手机和部门的原因是网站结构不好。这不是你的错。

© www.soinside.com 2019 - 2024. All rights reserved.