用python从html文件中提取zipcode

2024-06-18 13:45:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python从sec 10k html文件中提取Zipcode

我试过这个密码

import re
s="https://www.sec.gov/Archives/edgar/data/20/000095012310024631/c97665e10vk.htm"

zipcode = re.findall(r'\b[0-9]{5}(?:-[0-9]{4})?\b', s)
print zipcode

输出为[] 我需要08071-0888


Tags: 文件httpsimportre密码datahtmlwww
2条回答

[感谢您的帮助,我必须从文件夹中的文件数中提取zip和城市信息,我的代码如下,但会根据您的正则表达式进行更改。下一步是提取城市信息并将其保存到csv文件1

试试这个。首先,使用BeautifulSoup获取html。在html中查找所有td标记。然后,使用regex提取zipcode。你知道吗

from bs4 import BeautifulSoup
import requests, re

url = "https://www.sec.gov/Archives/edgar/data/20/000095012310024631/c97665e10vk.htm"

page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")

for s in soup.find_all("td", attrs={"align":"center"}):
    zipcode = re.findall("(\d{5}-\d{4})",str(s)) # you can also use your regex if you want
    if zipcode != []:
        print (zipcode)

输出:

['08071-0888']

相关问题 更多 >