[英]Extracting data from different type of html using beautifulsoup in python
I have the following types of HTML and I need to extract the "Student ID" from it.我有以下类型的 HTML,我需要从中提取“学生证”。 I could extract the student id from the HTML below, but I am not sure how can I modify my code so that I can correctly extract "Student ID" from the second type of HTML as well.
我可以从下面的 HTML 中提取学生 ID,但我不确定如何修改我的代码,以便我也可以从第二种类型的 HTML 中正确提取“学生 ID”。 Type1:
类型1:
student_html='''
<div style= "position:absolute; border:textbook 1px solid">
<span style="font-family: Helvetica; font-size:8px">
Student ID
<span style="font-family: Helvetica; font-size:8px">
123456
<br/>
</span>
</div>
<div style= "position:absolute; border:textbook 1px solid">
<span style="font-family: Helvetica; font-size:8px">
Student Name
<span style="font-family: Helvetica; font-size:8px">
John Doe
<br/>
</span>
</div>
'''
I am using the following code to extract the "Student ID" from the above HTML我正在使用以下代码从上面的 HTML 中提取“学生 ID”
from bs4 import BeautifulSoup
soup=BeautifulSoup(student_html,"lxml")
span_tags=soup.find_all("span")
for span in span_tags:
if span.text.strip()=="Student ID":
student_id=span.findNext("span").text
if span.text.strip()=="Student Name":
student_name=span.findNext("span").text
This is the second type of HTML.这是 HTML 的第二种类型。 Type2
类型2
type2HTML = '''<div style= "position:absolute; border:textbook 1px solid">
<span style="font-family: Helvetica; font-size:8px">
Student ID
<br/>
123456
<br/>
</span>
</div>
<div style= "position:absolute; border:textbook 1px solid">
<span style="font-family: Helvetica; font-size:8px">
Student Name
<br/>
John Doe
<br/>
</span>
</div>
'''
How can I modify the above code to extract the student ID from this?Similarly I need to extract other information:Student Name,Address, Grade etc如何修改上面的代码从中提取学生ID?同样我需要提取其他信息:学生姓名,地址,年级等
You could try this, once you have the right <div>
tags scooped out of the source HTML
.你可以试试这个,一旦你从源
HTML
中挖出正确的<div>
标签。
For example:例如:
from bs4 import BeautifulSoup
type_one = """
<div style= "position:absolute; border:textbook 1px solid">
<span style="font-family: Helvetica; font-size:8px">
Student ID
<span style="font-family: Helvetica; font-size:8px">
123456
<br/>
</span>
</div>"""
type_two = """<div style= "position:absolute; border:textbook 1px solid">
<span style="font-family: Helvetica; font-size:8px">
Student ID
<br/>
123456
<br/>
</span>
</div>
"""
all_types = [type_one, type_two]
for _type in all_types:
_id = (
BeautifulSoup(_type, "lxml")
.find("span")
.getText(strip=True, separator="|")
.split("|")[-1]
)
print(_id)
Output: Output:
123456
123456
If you're free to use other modules, consider the following solution:如果您可以自由使用其他模块,请考虑以下解决方案:
from weblib.etree import parse_html
from selection import XpathSelector
student_html='''
<div style= "position:absolute; border:textbook 1px solid">
<span style="font-family: Helvetica; font-size:8px">
Student ID
<span style="font-family: Helvetica; font-size:8px">
123456
<br/>
</span>
</div>'''
type2HTML = '''<div style= "position:absolute; border:textbook 1px solid">
<span style="font-family: Helvetica; font-size:8px">
Student ID
<br/>
123456
<br/>
</span>
</div>'''
all_types = [student_html, type2HTML]
for _type in all_types:
node = parse_html(_type)
nodes = [node for node in XpathSelector(node).select('//span')]
if len(nodes) == 1:
content = nodes[0].text()
else:
content = nodes[1].text()
student_id = content.replace('Student ID', '').strip()
print(student_id)
output output
123456
123456
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.