[問題] 郵局網路爬蟲輸出csv問題
各位大大好
小弟非本科 完全沒接觸過程式語言
最近因為工作關係開始學習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")
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.1627571581.A.78F.html
→
07/30 01:51,
3年前
, 1F
07/30 01:51, 1F
→
07/30 01:52,
3年前
, 2F
07/30 01:52, 2F
推
07/30 07:39,
3年前
, 3F
07/30 07:39, 3F
推
07/30 07:42,
3年前
, 4F
07/30 07:42, 4F
→
07/30 12:44,
3年前
, 5F
07/30 12:44, 5F
→
07/30 12:44,
3年前
, 6F
07/30 12:44, 6F
→
07/30 12:44,
3年前
, 7F
07/30 12:44, 7F
→
07/30 12:46,
3年前
, 8F
07/30 12:46, 8F
→
07/30 12:46,
3年前
, 9F
07/30 12:46, 9F
推
07/30 14:04,
3年前
, 10F
07/30 14:04, 10F
討論串 (同標題文章)
Python 近期熱門文章
PTT數位生活區 即時熱門文章