Skip to content

Instantly share code, notes, and snippets.

@ConteMan
Created June 11, 2022 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ConteMan/4a6bd7c421d6bd26a394975ffe8d3009 to your computer and use it in GitHub Desktop.
Save ConteMan/4a6bd7c421d6bd26a394975ffe8d3009 to your computer and use it in GitHub Desktop.
Douban movie page data to json
const elArr = document.querySelector('#wrapper > #content .subjectwrap .subject #info').innerHTML.toString().split('<br>');
const mDOMParser = new DOMParser();
const mData = {}
const title = document.querySelector('#wrapper > #content > h1 span[property="v:itemreviewed"]').innerText
Object.assign(mData, { '片名': title })
elArr.forEach(item => {
const mDom = mDOMParser.parseFromString(item, 'text/html')
const pl = mDom.querySelector('.pl')
if (!pl) return
const mKey = pl.innerText.replace(/:|:/, '')
if (!mKey || typeof mKey === 'undefined') return
let mValue = ''
if (/主演/.test(mKey)) {
const tempList = mDom.querySelectorAll('.attrs a[rel="v:starring"]')
const actors = []
tempList.forEach(actorItem => {
actors.push(actorItem.innerText)
})
mValue = actors
}
if (/IMDb|又名|制片国家|语言/.test(mKey)) {
const tempString = item.trim().replace(/^\<span.*\>.*\<\/span\>/, '').trim()
const tempArr = tempString.split('/')
if (tempArr.length > 1) {
mValue = tempArr.map(tempItem => tempItem.trim())
}
else {
mValue = tempString
}
}
if (/类型/.test(mKey)) {
const tempList = mDom.querySelectorAll('span[property="v:genre"]')
const temp = []
tempList.forEach(actorItem => {
temp.push(actorItem.innerText)
})
mValue = temp
}
if (!mValue && pl.nextElementSibling && !/官方网站/.test(mKey)) {
const tempString = pl.nextElementSibling.innerText
const tempArr = tempString.split('/')
if (tempArr.length > 1) {
mValue = tempArr.map(tempItem => tempItem.trim())
}
}
if (!mValue && pl.nextElementSibling) {
mValue = pl.nextElementSibling.innerText
}
mData[mKey] = mValue
})
const rateDom = document.querySelector('#wrapper > #content .subjectwrap > #interest_sectl')
const rating = rateDom.querySelector('strong.rating_num').innerText
const rateNumber = parseInt(rateDom.querySelector('a.rating_people > span[property="v:votes"]').innerText)
const summary = document.querySelector('#wrapper > #content .article .related-info .indent > span[property="v:summary"]').innerText
Object.assign(mData, { '评分': rating, '评分人数': rateNumber, '简介': summary })
console.log(mData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment