coding-coworking-club / basic-python-fall-2021

11 stars 6 forks source link

[General] GoogleAPI插件與CSV換行問題 #390

Open ricecake0329 opened 2 years ago

ricecake0329 commented 2 years ago
  1. 在Google Cloud Console啟動完API,即將與Python程式連結的時候,有顯現「插件不存在」的Error(ModuleNotFoundError: No module named 'Google'),想問不知道是缺少了什麼東西我沒有安裝或是設定到。

    import googleapiclient
    from apiclient.discovery import build
    from google_auth_oathlib.flow import InstalledAppFlow
    scopes = ['https://www.googleapis.com/auth/calendar',
    'https://www.googleapis.com/auth/calendar.events']
    flow = InstalledAppFlow.from_client_secrets_file('client_secret.json',scopes=scopes)
    flow.run_console()

    插件 上圖是已經安裝的插件,也有安裝oauth2client與Google的zip

  2. 將爬蟲成果建立成字典後,寫入CSV檔的時候會自動換行,已加入newlines之後依然沒有改變,想問是不是哪裡做錯了。

    import csv
    import re
    import requests
    from bs4 import BeautifulSoup                                           #插件安裝
    response = requests.get("https://www.indievox.com/tour")
    soup = BeautifulSoup(response.text, "html.parser")
    all_title = soup.find_all("div",class_="data") #展演名稱與標題
    subject = {"Subject":[]}
    starttime = {"Start Time":[]}
    startdate = {"Start Date":[]}
    enddate = {"End Date":[]}
    endtime = {"End Time":[]}
    response2 = requests.get("https://www.indievox.com/activity/detail/22_iV013016d")
    soup = BeautifulSoup(response2.text, "html.parser")
    all_title = soup.find_all("div",class_="col-md-9 col-xs-9")            #爬蟲展演名稱與標題
    for k in all_title:
    ptitle = k.find("h2",class_="title activity-title").text
    subject["Subject"] = (ptitle)
    all_time = soup.find_all("div",class_="tabCommon")                    #表演詳細資訊
    for time in all_time:
    activity_time = time.find("div", {"class": "tab-pane fade"}).text #取文字
    showtimeregex = re.compile(r"\D\D\D\D\D\D\D\D\d\d\d\d/\d\d/\d\d\D\D\D \d\d\D\d\d")
    showtime = showtimeregex.search(activity_time)
    startdateregex = re.compile(r'\d\d\d\d/\d\d/\d\d')
    startdate1 = startdateregex.search(activity_time)
    starttimeregex = re.compile(r'\d\d:\d\d')
    starttime2 = starttimeregex.search(activity_time)
    enddateregex = re.compile(r'\d\d\d\d/\d\d/\d\d')
    enddate3 = enddateregex.search(activity_time)
    endtimeregex = re.compile(r'\d\d:\d\d')
    endtime4 = starttimeregex.search(activity_time)
    showplace = re.search("LIVE WAREHOUSE",activity_time)
    singer = re.search("I Mean Us",activity_time)
    priceregex = re.compile(r"\D\D\D\d\d\d\D\D\D\D\D\d\d\d\D")#設定爬蟲參數
    price = priceregex.search(activity_time)
    sellingtimeregex = re.compile(r"\d\d\d\d\D\d\d\D\d\d\D\D\D \d\d\D\d\d")
    sellingtime = sellingtimeregex.search(activity_time)                  #建立字典
    startdate["Start Date"]= (startdate1.group())
    starttime["Start Time"] = (starttime2.group())
    enddate["End Date"] = (enddate3.group())
    endtime["End Time"] = (endtime4.group())
    dict_arr = [subject,startdate,starttime,enddate,endtime]
    labels = ['Subject','Start Date','Start Time','End Date','End Time']
    try:                                                                          #CSV檔寫入(需額外建立檔名為place_indi的csv檔)
    with open('place_indi.csv', 'w',newline='') as f:
        writer = csv.DictWriter(f, fieldnames=labels)                 
        writer.writeheader()
        for elem in dict_arr:
            writer.writerow(elem)
    except IOError:
    print("I/O error")

    印出來的成果會變成這樣 indi

    目前遇到的問題大概就是這樣,如果真的不行那也真的沒關係,我會再想辦法,感謝你能在百忙之中撥冗回答這些問題。

順頌時祺

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.