1 단계 파충류(3)

1387 단어 파충류xpath
한 토막:
import requests
url="https://en.wikipedia.org/wiki/Steve_Jobs"
res=requests.get(url)
print(res.status_code)
with open('a.html', 'w', encoding='utf-8') as f:
    f.write(res.text)

웹 페이지를 저장합니다. 윈도우즈와python 인코딩 때문에 오픈할 때 encoding='utf-8'
한 소절 더:
import requests
import re
from lxml import etree
with open("a.html","r",encoding="utf-8") as f:
    c=f.read()
tree=etree.HTML(c)
table_element=tree.xpath("//table[@class='infobox biography vcard']")
table_row=tree.xpath("//table[@class='infobox biography vcard'][1]/tbody/tr")
pattern_attrib=re.compile("<.>")
# print(table_element)
# infobox biography vcard
for row in table_row:
    try:
        thead=row.xpath("th")[0]
        title=etree.tostring(thead).decode("utf-8")
        title=pattern_attrib.sub(" ",title)
        desc=row.xpath("td")[0]
        desc=etree.tostring(desc).decode("utf-8")
        desc=pattern_attrib.sub(" ",desc)
        print(title+":"+desc)
        print("=========")
    except Exception as err:
        print(err)
        # pass
content=tree.xpath("//div[@id='mw-content-text'][1]//*[self::h2 or self::p]")
for line in content:
    print(line.text)

좋은 웹페이지 즐겨찾기