標籤

2022年11月21日 星期一

鉅亨新聞網-python解析新聞標題連結

 鉅亨新聞網-python解析新聞標題連結

最近cnyes 網頁格式變了

打開原始碼來看 長長的一大串,已不用a href 來顯示了

"5014707":{"isFetching":false,"didInvalidate":true,"items":{"newsId":5014707,"title":"全球貨櫃過剩 貨櫃車閒置 司機:空櫃快堆到門口","isIndex":1,"isCategoryHeadline":1,"hasCoverPhoto":1,"summary":"中國深圳部分堆場積壓大量貨櫃,貨櫃車司機表示,有些堆場都快堆到門口,且訂單偏少,凸顯全球海運業回歸常態,「一箱難求」已轉變為空櫃過剩。","payment":0,"publishAt":1669001573,"coverSrc":{"xs":{"src":"https:\u002F\u002Fci


從原始碼,找到重點要抓的就是 newsid,還有title, 新聞內容,可以自己用原來的網址再包裝

while True:
pos = page.text.find('"newsId"', pos + 1)
if pos == -1:
break
start = page.text.find(':', pos + 1)
if start == -1:
continue

end = page.text.find(',', pos + 1)
newsid = page.text[start+1:end]
pos = page.text.find('"title"', pos + 1)
if pos == -1:
break
start = page.text.find(':', pos + 1)
if start == -1:
continue

end = page.text.find(',', pos + 1)
title = page.text[start+2:end-1]

print(newsid,title)


所以我用土方法,來找字串,取得所要的資訊,就完工了

沒有留言:

張貼留言