Skip to content

Instantly share code, notes, and snippets.

@jzztf
Created May 21, 2019 22:55
Show Gist options
  • Save jzztf/009009ac6a55283f0133b0c5724b9c3e to your computer and use it in GitHub Desktop.
Save jzztf/009009ac6a55283f0133b0c5724b9c3e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import requests"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"s = requests.session()\n",
"r = s.get(\"https://github.com/events\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from lxml import html"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"root = html.fromstring(r.content)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['GitHub Events · GitHub']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"root.xpath(\"//title/text()\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Events']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"root.xpath(\"//h1/text()\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['/join?source=header',\n",
" '/features',\n",
" '/features/code-review/',\n",
" '/features/project-management/',\n",
" '/features/integrations',\n",
" '/features/actions',\n",
" '/features/package-registry',\n",
" '/features#team-management',\n",
" '/features#social-coding',\n",
" '/features#documentation',\n",
" '/features#code-hosting',\n",
" '/customer-stories',\n",
" '/security',\n",
" '/enterprise',\n",
" '/explore',\n",
" '/topics',\n",
" '/collections',\n",
" '/trending',\n",
" '/marketplace',\n",
" '/pricing',\n",
" '/pricing#feature-comparison',\n",
" '/nonprofit',\n",
" '/login?return_to=%2Fevents',\n",
" '/join?source=header',\n",
" '/explore',\n",
" '/topics',\n",
" '/collections',\n",
" '/events',\n",
" '/',\n",
" '/features',\n",
" '/security',\n",
" '/enterprise',\n",
" '/customer-stories',\n",
" '/pricing',\n",
" '/about/careers',\n",
" '/about/press',\n",
" '/site/terms',\n",
" '/site/privacy']"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 取属性,使用@符号,比如“@href”\n",
"root.xpath(\"//a[starts-with(@href, '/')]/@href\")"
]
},
{
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment