簡體   English   中英

多個for循環和csv文件

[英]Multiple for loops and csv files

我是python新手,現在還是新手,目前正在學習一些基本知識,主要是抓取,我遇到了一個問題,希望您能幫助我解決。

我正在嘗試從網站上抓取一些細節並將其寫入CSV文件,但是我只能將最后的結果寫入CSV,顯然我的腳本只是覆蓋了數據。

另外,如果您發現我的代碼有任何錯誤或有待改進的地方(我敢肯定有任何改進的余地),也很高興您也能指出這些錯誤。

Also2,任何對視頻/教程的建議都可以幫助我改善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()

謝謝托馬斯,實際上我想了一下如何簡化代碼(四個for循環太多了,不是嗎?),所以對代碼進行了一些調整,因此使用以下代碼解決了我的問題(刪除了“部門”和“電話”,因為其他一些問題):

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

嗨,Babr歡迎使用python。 您的回答很好,這是您可以做得更好的另一件事。

如果只需要一個元素,請使用find replace 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()

而您需要刪除電話和部門的原因是由於不良的網站結構。 這不是你的錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM