iT邦幫忙

0

求改成用字典的方法來改成可以把空位欄填對

  • 分享至 

  • twitterImage


import requests
from bs4 import BeautifulSoup
import csv


url = "https://csie.asia.edu.tw/faculty/professors"

r = requests.get(url)
r.encoding = "utf8"
soup = BeautifulSoup(r.text, "lxml")


tag_table = soup.find(attrs={"class":"row contact-category"}) 

ALL_Professors = tag_table.find_all("div",class_="col-sm-12")


csvfile = "AU_CSIE_Professor_jdwang2020_5_20_BIG5_FieldName.csv"


with open(csvfile, 'w+', newline='', encoding="big5") as fp:
    writer = csv.writer(fp)
    
    Fieldnames = ["姓名","學歷","辦公室","分機","E-mail"]
    writer.writerow(Fieldnames)
    for One in ALL_Professors:
        OneRecordList = []
        print (One.find('h2','card-header').string)
        OneRecordList.append(One.find('h2','card-header').string)
        ALL_card_description = One.find_all("p",class_="card-description")
       
        for oneItem in ALL_card_description:
            OneRecordList.append(oneItem.text.split(':')[1].replace("\t","").replace(" ",""))
        print(OneRecordList)
        writer.writerow(OneRecordList)
            
       
       


https://ithelp.ithome.com.tw/upload/images/20200529/20127515biREpkXWZ2.jpg

請問爬蟲這個紅色箭頭指分機的要怎把它改成可以填到信箱那一格,用list會沒辦法移過去,想請問怎用字典移過去,求大神。

p.s不好意思第一次發文,已發出程式碼。

看更多先前的討論...收起先前的討論...
slime iT邦大師 1 級 ‧ 2020-05-29 11:05:43 檢舉
我覺得問題在於"辦公室"沒有値, 所以"分機"的內容跑到辦公室了.
dragonH iT邦超人 5 級 ‧ 2020-05-29 11:22:06 檢舉
現在用圖片 coding 是主流嗎
淺水員 iT邦大師 6 級 ‧ 2020-05-29 11:27:26 檢舉
這個版有貼程式碼的功能
請直接貼上程式碼而不是圖片喔
(一般看到圖片都會比較懶得回答,因為要測試還得自己打一次程式碼)
淺水員 iT邦大師 6 級 ‧ 2020-05-29 11:29:35 檢舉
另外第二張圖片建議撤掉,換一張假資料比較好...
(雖然本來就查得到,好吧,我也不知道)
淺水員 iT邦大師 6 級 ‧ 2020-05-30 12:41:04 檢舉
可以預先留空位
OneRecordList = ['', '', '', '', '']
然後如果取到姓名就填
OneRecordList[0]=姓名
然後如果取到學歷就填
OneRecordList[1]=學歷
以此類推
應該也能加上if來判斷空值就填上空格或其他。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答