【发布时间】:2021-02-03 06:17:16
【问题描述】:
扫描器一直工作,直到找到不再可用的外部地址,然后崩溃。
我只想扫描 herold.at 并提取电子邮件地址。
我希望他停止扫描外部地址。我试过了
r = requests.get ('http://github.com', allow_redirects = False) 但不起作用。
import csv
import requests
import re
import time
from bs4 import BeautifulSoup
# Number of pages plus one
allLinks = [];mails=[];
url = 'https://www.herold.at/gelbe-seiten/wien/was_installateur/?page='
for page in range(3):
time.sleep(5)
print('---', page, '---')
response = requests.get(url + str(page), timeout=1.001)
soup=BeautifulSoup(response.text,'html.parser')
links = [a.attrs.get('href') for a in soup.select('a[href]') ]
for i in links:
#time.sleep(15)
if(("Kontakt" in i or "Porträt")):
allLinks.append(i)
allLinks=set(allLinks)
def findMails(soup):
#time.sleep(15)
for name in soup.find_all("a", "ellipsis"):
if(name is not None):
emailText=name.text
match=bool(re.match('[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+',emailText))
if('@' in emailText and match==True):
emailText=emailText.replace(" ",'').replace('\r','')
emailText=emailText.replace('\n','').replace('\t','')
if(len(mails)==0)or(emailText not in mails):
print(emailText)
mails.append(emailText)
for link in allLinks:
if(link.startswith("http") or link.startswith("www")):
r=requests.get(link)
data=r.text
soup=BeautifulSoup(data,'html.parser')
findMails(soup)
else:
newurl=url+link
r=requests.get(newurl)
data=r.text
soup=BeautifulSoup(data,'html.parser')
findMails(soup)
mails=set(mails)
if(len(mails)==0):
print("NO MAILS FOUND")
错误:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.gebrueder-lamberger.at', port=80): 最大重试次数超出 url: / (由 NewConnectionError('
【问题讨论】:
-
听起来您需要添加一些代码来过滤掉来自
allLinks的不需要的链接,对吧? -
对,我不需要扫描外部域。我只需要 herold.at
-
是的。您已经有了通过地址递归的代码。是什么阻止您在那里添加过滤器?
-
另外,您的代码无法防止重新扫描已扫描的 URL。
-
我不知道要添加什么。你能帮帮我吗?
标签: python