簡體   English   中英

Python 2.7 BeautifulSoup,電子郵件抓取

[英]Python 2.7 BeautifulSoup , email scraping

希望你一切都好。 我是使用Python 2.7的Python新手。

我正在嘗試僅從此公共網站業務目錄中提取mailto: http : //www.tecomdirectory.com/companies.php? segment=&activity=&activity=&search=category&submit=Search
我要查找的郵件是完整目錄中az中每個小部件中提到的電子郵件。 不幸的是,該目錄沒有API。 我正在使用BeautifulSoup,但到目前為止沒有成功。
這是mycode:

import urllib
from bs4 import BeautifulSoup
website = raw_input("Type website here:>\n")
html = urllib.urlopen('http://'+ website).read()
soup = BeautifulSoup(html)

tags = soup('a') 

for tag in tags:
    print tag.get('href', None)

我得到的只是實際網站的網站,例如帶有其他href的http://www.tecomdirectory.com ,而不是小部件中的mailto或網站。 我還嘗試用湯(“目標”)代替湯(“ a”),但沒有運氣! 有人可以幫我嗎?

您不僅可以找到每個錨,還需要在href中專門查找“ mailto:”,可以使用css選擇器a[href^=mailto:]查找具有以mailto:開頭的href的 標簽:

import requests

soup  = BeautifulSoup(requests.get("http://www.tecomdirectory.com/companies.php?segment=&activity=&search=category&submit=Search").content)

print([a["href"] for a in soup.select("a[href^=mailto:]")])

或提取文本:

print([a.text for a in soup.select("a[href^=mailto:]")])

使用find_all("a")您將需要使用正則表達式來實現相同目的:

import re

find_all("a", href=re.compile(r"^mailto:"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM