pythonres
發布時間: 2023-07-16 19:27:27
1. 如何用python爬蟲抓取網頁內容
首先,你要安裝requests和BeautifulSoup4,然後執行如下代碼.
importrequests
frombs4importBeautifulSoup
iurl='http://news.sina.com.cn/c/nd/2017-08-03/doc-ifyitapp0128744.shtml'
res=requests.get(iurl)
res.encoding='utf-8'
#print(len(res.text))
soup=BeautifulSoup(res.text,'html.parser')
#標題
H1=soup.select('#artibodyTitle')[0].text
#來源
time_source=soup.select('.time-source')[0].text
#來源
origin=soup.select('#artibodyp')[0].text.strip()
#原標題
oriTitle=soup.select('#artibodyp')[1].text.strip()
#內容
raw_content=soup.select('#artibodyp')[2:19]
content=[]
forparagraphinraw_content:
content.append(paragraph.text.strip())
'@'.join(content)
#責任編輯
ae=soup.select('.article-editor')[0].text
這樣就可以了
2. python列印二叉樹所有路徑的主函數怎樣寫
基本演算法就是二叉樹的遍歷,首先想到的是深度優先遍歷。
難點在於,如何實現每個子路徑的記錄和append
binaryTreePaths函數只給了root變數,無法存儲每個子路徑,考慮寫輔助函數res,添加存儲路徑的變數
res(root,temp)
同時還需要一個全局變數result存儲最後的輸出結果,result.append(temp)
熱點內容