计算机类 专业技术中级资格 数据库系统工程师 应用技术
数据采集 操作题 4 题 含答案 应用技术
1 材料分析题 1 / 4
用Beautiful Soup 装载下面的 HTML 文档,并以规范的格式输出。 <body> <div>Hi<br> <span>Hello</SPAN> <p><div>End</div>
查看答案 from bs4 import BeautifulSoup html=''' <body> <div>Hi<br> <span>Hello</SPAN> <p><div>End</div> ''' soup=BeautifulSoup(html,"lxml") print(soup.prettify())
2 材料分析题 2 / 4
下面是一段 HTML 代码: <body> <bookstore> <book id="b1"> <title lang="english">Harry Potter</title> <price>23.99</price> </book> <book id="b2"> <title lang="chinese">学习 XML</title> <price>39.95</price> </book> <book id="b3"> <title lang="english">Learning Python</title> <price>30.20</price> </book> </bookstore> </body></html> 试用 Beautiful Soup 找出所有书的名称。
查看答案 from bs4 import BeautifulSoup html=''' <body> <bookstore> <book id="b1"> <title lang="english">Harry Potter</title> <price>23.99</price> </book> <book id="b2"> <title lang="chinese">学习 XML</title> <price>39.95</price> </book> <book id="b3"> <title lang="english">Learning Python</title> <price>30.20</price> </book> </bookstore> </body></html> ''' soup=BeautifulSoup(html,"html.parser") books=soup.find_all("book") for b in books: print(b.find("title").text)
3 材料分析题 3 / 4
下面是一段 HTML 代码: <body> <bookstore> <book id="b1"> <title lang="english">Harry Potter</title> <price>23.99</price> </book> <book id="b2"> <title lang="chinese">学习 XML</title> <price>39.95</price> </book> <book id="b3"> <title lang="english">Learning Python</title> <price>30.20</price> </book> </bookstore> </body></html> 试用 BeautifulSoup 找出价格在 30 元以上的所有书的名称。
查看答案 from bs4 import BeautifulSoup html=''' <body> <bookstore> <book id="b1"> <title lang="english">Harry Potter</title> <price>23.99</price> </book> <book id="b2"> <title lang="chinese">学习 XML</title> <price>39.95</price> </book> <book id="b3"> <title lang="english">Learning Python</title> <price>30.20</price> </book> </bookstore> </body></html> ''' soup=BeautifulSoup(html,"html.parser") books=soup.find_all("book") for b in books: price=b.find("price").text if float(price)>30: print(b.find("title").text)
4 材料分析题 4 / 4
使用正则表达式匹配找出英文句子“I am testing search function”中的所有单词。
查看答案 import re s = "I am testing search function" reg = r"[A-Za-z]+\b" m = re.search(reg,s) while m!=None: start=m.start() end=m.end() print(s[start:end]) s=s[end:] m=re.search(reg,s)