Skip to content

Instantly share code, notes, and snippets.

@andy23512
Last active March 9, 2018 10:50
Show Gist options
  • Save andy23512/4cbfb7a24a22a900c59e60abf3abe4c5 to your computer and use it in GitHub Desktop.
Save andy23512/4cbfb7a24a22a900c59e60abf3abe4c5 to your computer and use it in GitHub Desktop.
Use regex to search text in html via BeautifulSoup
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"\n",
"res = requests.get(\"http://civil.csu.edu.tw/wSite/lp?ctNode=13264&mp=541613&idPath=8859_13263_13264\")\n",
"res.encoding = 'utf8'\n",
"soup = BeautifulSoup(res.text,'html.parser')\n",
"web_tag = 'div.article a'\n",
"articles = soup.select(web_tag)\n",
"email_set = set()\n",
"for art in articles:\n",
" res2 = requests.get(\"http://civil.csu.edu.tw/wSite/\"+art['href'])\n",
" res2.encoding = 'utf8'\n",
" soup2 = BeautifulSoup(res2.text,'html.parser')\n",
" pattern = re.compile(r'.*@.*')\n",
" email_set.add(soup2.find(text=pattern))\n",
"print(email_set)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment