【问题标题】:Multiple for loops and csv files多个 for 循环和 csv 文件
【发布时间】:2019-08-04 16:58:55
【问题描述】:

我是新来的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()

【问题讨论】:

  • 你的线索在方法名csv_writer.writerow([department_name, contact_name, position, first_phones])中。 writerow 写一行,你调用一次。例如,department_name 的值在 for department in soup.find_all("div", class_="view-content"): 中经历了它可能拥有的所有化身,没有写入,剩下的就是它的最后一个化身。

标签: python python-2.7 web-scraping beautifulsoup


【解决方案1】:

谢谢托马斯, 实际上,我通过思考如何使它更简单来稍微调整我的代码(四个 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()

【讨论】:

    【解决方案2】:

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

    而您需要放弃电话和部门的原因是因为网站结构不好。这不是你的错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-25
      • 2018-01-12
      • 2011-09-02
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2019-07-21
      • 2015-10-25
      相关资源
      最近更新 更多