用python搜索html

用python搜索html,python,html,search,urllib,Python,Html,Search,Urllib,我的问题是关于用Python搜索html格式。 我正在使用以下代码: with urllib.request.urlopen("http://") as url: data = url.read().decode() 现在,这将返回页面中的全部HTML代码,我想提取所有电子邮件地址 谁能帮我一下吗? 提前感谢请记住,您不应该使用正则表达式进行实际的HTML解析(谢谢@Patrick Artner),但您可以使用Beauty soup提取网页上的所有可见文本或注释。然后,您可以使用此文本

我的问题是关于用Python搜索html格式。 我正在使用以下代码:

with urllib.request.urlopen("http://") as url:
    data = url.read().decode()
现在,这将返回页面中的全部HTML代码,我想提取所有电子邮件地址

谁能帮我一下吗?
提前感谢

请记住,您不应该使用正则表达式进行实际的HTML解析(谢谢@Patrick Artner),但您可以使用Beauty soup提取网页上的所有可见文本或注释。然后,您可以使用此文本(只是一个字符串)查找电子邮件地址。以下是您如何做到这一点:

from bs4 import BeautifulSoup
from bs4.element import Comment
import urllib
import re

def tag_visible(element):
    if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
        return False
    if isinstance(element, Comment):
        return False
    return True


def text_from_html(body):
    soup = BeautifulSoup(body, 'html.parser')
    texts = soup.findAll(text=True)
    visible_texts = filter(tag_visible, texts)
    return u" ".join(t.strip() for t in visible_texts)

with urllib.request.urlopen("https://en.wikipedia.org/wiki/Email_address") as url:
    data = url.read().decode()
    text = text_from_html(data)
    print(re.findall(r"[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*", text))
这两个helper函数只是获取页面上可以看到的所有文本,然后荒谬的长正则表达式只是从该文本中提取所有电子邮件地址。我以wikipedia.com关于电子邮件的文章为例,下面是输出:

['John.Smith@example.com', 'local-part@domain', 'jsmith@example.com', 'john.smith@example.org', 'local-part@domain', 'John..Doe@example.com', 'fred+bah@domain', 'fred+foo@domain', 'fred@domain', 'john.smith@example.com', 'john.smith@example.com', 'jsmith@example.com', 'JSmith@example.com', 'john.smith@example.com', 'john.smith@example.com', 'prettyandsimple@example.com', 'very.common@example.com', 'disposable.style.email.with+symbol@example.com', 'other.email-with-dash@example.com', 'fully-qualified-domain@example.com', 'user.name+tag+sorting@example.com', 'user.name@example.com', 'x@example.com', 'example-indeed@strange-example.com', 'admin@mailserver1', "#!$%&'*+-/=?^_`{}|~@example.org", 'example@s.solutions', 'user@localserver', 'A@b', 'c@example.com', 'l@example.com', 'right@example.com', 'allowed@example.com', 'allowed@example.com', '1234567890123456789012345678901234567890123456789012345678901234+x@example.com', 'john..doe@example.com', 'example@localhost', 'john.doe@example', 'joeuser+tag@example.com', 'joeuser@example.com', 'foo+bar@example.com', 'foobar@example.com']

请记住,您不应该使用正则表达式进行实际的HTML解析(谢谢@Patrick Artner),但您可以使用Beauty soup提取网页上的所有可见文本或注释。然后,您可以使用此文本(只是一个字符串)查找电子邮件地址。以下是您如何做到这一点:

from bs4 import BeautifulSoup
from bs4.element import Comment
import urllib
import re

def tag_visible(element):
    if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
        return False
    if isinstance(element, Comment):
        return False
    return True


def text_from_html(body):
    soup = BeautifulSoup(body, 'html.parser')
    texts = soup.findAll(text=True)
    visible_texts = filter(tag_visible, texts)
    return u" ".join(t.strip() for t in visible_texts)

with urllib.request.urlopen("https://en.wikipedia.org/wiki/Email_address") as url:
    data = url.read().decode()
    text = text_from_html(data)
    print(re.findall(r"[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*", text))
这两个helper函数只是获取页面上可以看到的所有文本,然后荒谬的长正则表达式只是从该文本中提取所有电子邮件地址。我以wikipedia.com关于电子邮件的文章为例,下面是输出:

['John.Smith@example.com', 'local-part@domain', 'jsmith@example.com', 'john.smith@example.org', 'local-part@domain', 'John..Doe@example.com', 'fred+bah@domain', 'fred+foo@domain', 'fred@domain', 'john.smith@example.com', 'john.smith@example.com', 'jsmith@example.com', 'JSmith@example.com', 'john.smith@example.com', 'john.smith@example.com', 'prettyandsimple@example.com', 'very.common@example.com', 'disposable.style.email.with+symbol@example.com', 'other.email-with-dash@example.com', 'fully-qualified-domain@example.com', 'user.name+tag+sorting@example.com', 'user.name@example.com', 'x@example.com', 'example-indeed@strange-example.com', 'admin@mailserver1', "#!$%&'*+-/=?^_`{}|~@example.org", 'example@s.solutions', 'user@localserver', 'A@b', 'c@example.com', 'l@example.com', 'right@example.com', 'allowed@example.com', 'allowed@example.com', '1234567890123456789012345678901234567890123456789012345678901234+x@example.com', 'john..doe@example.com', 'example@localhost', 'john.doe@example', 'joeuser+tag@example.com', 'joeuser@example.com', 'foo+bar@example.com', 'foobar@example.com']
使用beautifulsoup,您可以执行以下操作:

import requests
from bs4 import BeautifulSoup
import re

response = requests.get("your_url")
response_text = response.text
beautiful_response = BeautifulSoup(response_text, 'html.parser')

email_regex = r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'

list_of_emails = re.findall(email_regex, beautiful_response .text)
list_of_emails_decoded = []
for every_email in list_of_emails:
    list_of_emails_decoded.append(every_email.encode('utf-8'))
使用beautifulsoup,您可以执行以下操作:

import requests
from bs4 import BeautifulSoup
import re

response = requests.get("your_url")
response_text = response.text
beautiful_response = BeautifulSoup(response_text, 'html.parser')

email_regex = r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'

list_of_emails = re.findall(email_regex, beautiful_response .text)
list_of_emails_decoded = []
for every_email in list_of_emails:
    list_of_emails_decoded.append(every_email.encode('utf-8'))

查看。如果你能提供一个返回数据的示例,可能会有所帮助,这样我们就可以帮助你找出如何解析电子邮件地址。怎么样?只是不要思考、提及、建议或使用regex或我很久没见过zalgo的人;PCheck out。如果你能提供一个返回数据的示例,这可能会很有帮助,这样我们就可以帮助你找出如何解析电子邮件地址。怎么样?只是不要思考、提及、建议或使用regex或man,我已经有一段时间没见过zalgo了;你反应太快了!还有很好的答案。他们的工作,我明白如何!只需要找出你使用的一些术语,但这就是duckduckgo的用途。谢谢你的努力,你反应这么快!还有很好的答案。他们的工作,我明白如何!只需要找出你使用的一些术语,但这就是duckduckgo的用途。谢谢你所有的努力