먼저 파이썬을 처음 접했습니다. 오프라인 웹 사이트에서 연락처 정보를 긁어내어 CSV로 정보를 출력하려고합니다. 가능한 경우 HTML, HTML, 전자 메일, 전화 번호, 위치 데이터를 포함하는 페이지 URL을 가져오고 싶습니다. 모든 이름, 모든 전화 번호 및 HTML 사이트가있는 경우 태그 줄을 가져오고 싶습니다.* 업데이트 : python/beautifulsoup으로 html을 파싱하는 방법
업데이트 # 2 코드 :
는import os, csv, re
from bs4 import BeautifulSoup
topdir = 'C:\\projects\\training\\html'
output = csv.writer(open("scrape.csv", "wb+"))
output.writerow(["headline", "name", "email", "phone", "location", "url"])
all_contacts = []
for root, dirs, files in os.walk(topdir):
for f in files:
if f.lower().endswith((".html", ".htm")):
soup = BeautifulSoup(f)
def mailto_link(soup):
if soup.name != 'a':
return None
for key, value in soup.attrs:
if key == 'href':
m = re.search('mailto:(.*)',value)
if m:
all_contacts.append(m)
return m.group(1)
return None
for ul in soup.findAll('ul'):
contact = []
for li in soup.findAll('li'):
s = li.find('span')
if not (s and s.string):
continue
if s.string == 'Email:':
a = li.find(mailto_link)
if a:
contact['email'] = mailto_link(a)
elif s.string == 'Website:':
a = li.find('a')
if a:
contact['website'] = a['href']
elif s.string == 'Phone:':
contact['phone'] = unicode(s.nextSibling).strip()
all_contacts.append(contact)
output.writerow([all_contacts])
print "Finished"
이 출력 현재 행 머리글 이외의 아무것도 반환하지 않습니다. 내가 여기서 무엇을 놓치고 있니? 적어도 html 파일에서 정보를 반환해야합니다.이 페이지는 다음과 같습니다. http://bendoeslife.tumblr.com/about
일반적으로 페이지 HTML에서 페이지 URL을 가져올 수 없습니다. 가져 오기 시간에 저장해야합니다. 나머지는 ... 파서의 잘못된 점을 알려주기 위해 샘플 데이터를 볼 필요가 있습니다. – abarnert