Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用regex Python3时超时_Html_Python 3.x_Web Scraping_Re - Fatal编程技术网

Html 使用regex Python3时超时

Html 使用regex Python3时超时,html,python-3.x,web-scraping,re,Html,Python 3.x,Web Scraping,Re,我试图用正则表达式将电子邮件转换成html,但我在一些网站上遇到了问题 主要的问题是正则表达式函数使进程瘫痪,使cpu过载 import re from urllib.request import urlopen, Request email_regex = re.compile('([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})', re.IGNORECASE) request = Request('http://www.serviciositvyecla.

我试图用正则表达式将电子邮件转换成html,但我在一些网站上遇到了问题

主要的问题是正则表达式函数使进程瘫痪,使cpu过载

import re
from urllib.request import urlopen, Request

email_regex = re.compile('([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})', re.IGNORECASE)

request = Request('http://www.serviciositvyecla.com')
request.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36')
html = str(urlopen(request, timeout=5).read().decode("utf-8", "strict"))

email_regex.findall(html) ## here is where regex takes a long time

我没有问题,如果该网站是另一个

request = Request('https://www.velezmalaga.es/')
如果有人知道如何解决这个问题,或者知道如何超时regex函数,我会非常感激


我使用Windows。

我最初试图摆弄你的方法,但后来我放弃了,转而使用了
beautifulsou
。成功了

试试这个:

import re
import requests

from bs4 import BeautifulSoup


headers = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
                  "(KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
}

pages = ['http://www.serviciositvyecla.com', 'https://www.velezmalaga.es/']

emails_found = set()
for page in pages:
    html = requests.get(page, headers=headers).content
    soup = BeautifulSoup(html, "html.parser").select('a[href^=mailto]')
    for item in soup:
        try:
            emails_found.add(item['href'].split(":")[-1].strip())
        except ValueError:
            print("No email :(")

print('\n'.join(email for email in emails_found))
输出:

info@serviciositvyecla.com
oac@velezmalaga.es
编辑:

你的方法不起作用的一个原因是正则表达式本身。另一个是返回的HTML的大小(我怀疑)

见此:

import re
import requests

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
                  "(KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
}

html = requests.get('https://www.velezmalaga.es/', headers=headers).text

op_regx = '([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})'
simplified_regex = '[\w\.-]+@[\w\.-]+\.\w+'

print(f"OP's regex results: {re.findall(op_regx, html)}")
print(f"Simplified regex results: {re.findall(simplified_regex, html)}")
这张照片是:

OP's regex results: []
Simplified regex results: ['oac@velezmalaga.es', 'oac@velezmalaga.es']

最后,我通过正则表达式搜索找到了一个不消耗所有RAM的解决方案。在我的问题中,即使网络上有电子邮件,也获得白色结果是一个可接受的解决方案,只要不因内存不足而阻塞进程。 该页面的html包含550万个字符。510万不包含优先级信息,因为它是一个隐藏的div,具有无法理解的字符。 我添加了一个类似以下情况的例外:
如果len(html)<1000000:do thever

你能在email\u regex中发布一个html内容的示例吗。findall(html)不,html是广泛的。请运行代码,直到代码中包含html。嗨,巴杜克,谢谢你的回复。在编辑解决方案中,您给了我“”的响应,但对“”没有响应。它使进程瘫痪并离开cpuoverloaded@JoakinMontesinos答案的第一部分是两封电子邮件。我还写了一些可能的原因,说明您的代码可能无法按预期的方式工作。当然,bs4的解决方案是可行的,但这不是我想要的,因为您已经应用了规则
。select('a[href^=mailto]')
。思想​​在html中使用reguex并不是为了找到标记。@JoakinMontesinos首先它是
regex
而不是
reguex
。另一件事是,用正则表达式解析HTML是不可能的,因为它依赖于匹配开头和结尾标记,而这在正则表达式中是不可能的。有更好的工具,如BS4或lxml。请在这里阅读更多信息-哦,对不起,输入错误和链接太棒了。尽管阅读,我还是想继续使用正则表达式。当regex冻结进程并导致CPU过载时,是否有一个超时转义regex的解决方法?即使答案是空的