[英]Scrape string of text from website with beautiful soup
我想抓取一個網頁,然后只返回GTM(Google跟蹤代碼管理器)容器ID(在下面的示例中為GTM-5LS3NZ)。 代碼不應該查找確切的容器ID,而應該查找模式,因為我將在多個站點上使用它。
到目前為止,我可以搜索標題並打印包含GTM的整個文本,但是我不知道如何將搜索結果和正則表達式一起格式化以僅返回GTM-5LS3NZ(在此示例中)。
import urllib3
import re
from bs4 import BeautifulSoup
http = urllib3.PoolManager()
response = http.request('GET', "https://www.observepoint.com/")
soup = BeautifulSoup(response.data,"html.parser")
GTM = soup.head.findAll(text=re.compile(r'GTM'))
print(GTM)
注意:GTM ID可以包含6或7個字母數字字符,因此我希望容器ID的正則表達式類似於^ GTM- [A-Z0-9]-我不知道如何指定6或7個字符。
澄清我的追求。 如果運行上面的代碼,則會得到以下內容。
["(function (w, d, s, l, i) {\n w[l] = w[l] || [];\n w[l].push({\n 'gtm.start': new Date().getTime(),\n event: 'gtm.js'\n });\n var f = d.getElementsByTagName(s)[0],\n j = d.createElement(s),\n dl = l != 'dataLayer' ? '&l=' + l : '';\n j.async = true;\n j.src =\n 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;\n f.parentNode.insertBefore(j, f);\n })(window, document, 'script', 'dataLayer', 'GTM-5LS3NZ');"]
我要的是GTM-5LS3NZ。
感謝評論中的幫助,我現在已經解決了。 這就是我所追求的:
import re
from bs4 import BeautifulSoup
http = urllib3.PoolManager()
response = http.request('GET', "https://www.observepoint.com/")
soup = BeautifulSoup(response.data,"html.parser")
GTM = soup.head.findAll(text=re.compile(r'GTM'))
print(re.search("GTM-[A-Z0-9]{6,7}",str(GTM))[0])
我幾天前做了類似的事情,然后快速重寫就給了我:
import urllib3
import re
from bs4 import BeautifulSoup
http = urllib3.PoolManager()
response = http.request('GET', "https://www.observepoint.com/")
soup = BeautifulSoup(response.data,"html.parser")
pattern =re.compile(r'GTM-([a-zA-Z0-9]{6,7})')
found = soup.head.find(text=pattern)
if found:
match = pattern.search(found)
if match:
print(match.group(1))
這給了我GTM-5LS3NZ作為輸出。
您也可以從適當的評論中提取
import requests
from bs4 import BeautifulSoup, Comment
r = requests.get('https://www.observepoint.com/')
soup = BeautifulSoup(r.content, 'lxml')
for comment in soup.findAll(text=lambda text:isinstance(text, Comment)):
if 'iframe' in comment:
soup = BeautifulSoup(comment, 'lxml')
id = soup.select_one('iframe')['src'].split('=')[1]
print(id)
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.