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)
热点内容