[問題] 網路爬蟲函式問題已刪文

看板Python作者 (嘎哩)時間3年前 (2021/07/29 20:06), 編輯推噓1(102)
留言3則, 2人參與, 3年前最新討論串1/1
各位大大好 小弟非本科 完全沒接觸過程式語言 最近因為工作關係開始學習python 目前只要使用爬蟲相關 日前爬了郵局的網站 但是在局名的部分似乎有點問題 檔案輸出後通通都變成同一間(新莊副都心) 另外兩行的資料是正確的 想請問大家能否協助我修改 謝謝各位 以下附上函式 ——————————————- import urllib.request as req import pandas as pd import csv data = {'縣市':[""],'局名':[""],'地址':[""] } storelist = pd.DataFrame(data) storelist_ = pd.DataFrame(data) url="https://www.post.gov.tw/post/internet/I_location/index_all.html" request=req.Request(url, headers={ "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" }) with req.urlopen(request) as response: data=response.read().decode("utf-8") #解析原始碼,取得每篇文章的標題 import bs4 root=bs4.BeautifulSoup(data, "html.parser") titles=root.find_all("td", class_="detail2") #尋找class="title"的div標籤 for title in titles: if title.a !=None: data2=title.a.string adds=root.find_all("td",class_="detail") for add in adds: data1=add.string[0:3] data3=add.string storelist_['縣市']=data1 storelist_['局名']=data2 storelist_['地址']=data3 storelist = storelist.append(storelist_) #一筆一筆加入清單中 storelist.to_csv("D:\Mypython/test.csv",encoding="utf-8-sig") -- Sent from nPTT on my iPhone 7 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.171.149.6 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1627560364.A.5C8.html

07/29 20:13, 3年前 , 1F
class_="detail2") #尋找class="title" 你不覺得哪裡怪怪的?
07/29 20:13, 1F

07/29 20:34, 3年前 , 2F
抱歉那個#後面是參考彭彭那邊寫出來的 忘記刪掉 我是
07/29 20:34, 2F

07/29 20:34, 3年前 , 3F
拿他的函式去改的 #再幫我忽略感謝
07/29 20:34, 3F
文章代碼(AID): #1X0fciN8 (Python)
文章代碼(AID): #1X0fciN8 (Python)