我想剪贴this website中的电子邮件,但它们受到保护。它们在网站上可见,但是在抓取受保护的电子邮件时会出现,这些电子邮件将被解码。
我尝试刮刮,但是得到了这个结果
<a href="/cdn-cgi/l/email-protection#d5a7bba695b9a6b0b2fbb6bab8"><span class="__cf_email__" data-cfemail="c0b2aeb380acb3a5a7eea3afad">[email protected]</span></a>
我的代码:
from bs4 import BeautifulSoup as bs
import requests
import re
r = requests.get('https://www.accesswire.com/api/newsroom.ashx')
p = re.compile(r" \$\('#newslist'\)\.after\('(.*)\);")
html = p.findall(r.text)[0]
soup = bs(html, 'lxml')
headlines = [item['href'] for item in soup.select('a.headlinelink')]
for head in headlines:
response2 = requests.get(head, headers=header)
soup2 = bs(response2.content, 'html.parser')
print([a for a in soup2.select("a")])
我想要正文中的电子邮件,例如电子邮件:theramedhealthcorp@gmail.com 来自此网站https://www.accesswire.com/546295/Theramed-Provides-Update-on-New-Sales-Channel-for-Nevada-Facility的电子邮件 但是电子邮件受到保护,如何以文本形式(如真实电子邮件地址)将其删除? 谢谢
答案 0 :(得分:0)
我首先尝试了您的代码,但我也得到了[电子邮件保护]
然后我意识到网站可能正在通过JavaScript加载数据。
您可以使用硒或任何简便的浏览器完成工作。
我已经使用PyQt5库打开页面,因为它将在启用JavaScript的浏览器中打开,然后从中获取源代码并执行常规的BeautifulSoup代码。
必备安装命令(如果您是Windows用户):
要安装PyQt5:pip install pyqt5
PyQt5 Windows发行版没有PyQtWebEngine,我们需要单独安装:
pip install PyQtWebEngine
要使用pyqt4渲染基于JavaScript的页面,我在这里关注了SentDex的视频:https://www.youtube.com/watch?v=FSH77vnOGqU
但这是针对pyqt4的。为了从pyqt4过渡到pyqt5,此StackOverflow答案对我有所帮助:
https://stackoverflow.com/a/44432380/8810517
我的代码:
import requests
import re
from bs4 import BeautifulSoup as bs
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage
class Client(QWebEnginePage):
def __init__(self,url):
self.app = QApplication(sys.argv)
QWebEnginePage.__init__(self)
self.html=""
self.loadFinished.connect(self.on_page_load)
self.load(QUrl(url))
self.app.exec_()
def on_page_load(self):
self.html=self.toHtml(self.Callable)
print("In on_page_load \n \t HTML: ",self.html)
def Callable(self,html_str):
print("In Callable \n \t HTML_STR: ",len(html_str))
self.html=html_str
print("In Callable \n \t HTML_STR: ",len(self.html))
self.app.quit()
url="https://www.accesswire.com/546227/InterRent-Announces-Voting-Results-from-the-2019-Annual-and-Special-Meeting"
client_response= Client(url)
soup = bs(client_response.html, 'html.parser')
table = soup.find_all('table')
#print(len(table))
table = table[len(table)-1]
#print(table)
a = table.find_all('a')
#print(len(a))
for i in a:
print(i.text)
输出:
mmcgahan@interrentreit.com
bcutsey@interrentreit.com
cmillar@interrentreit.com