python爬虫之获取页面script里面的内容
2024-08-27 20:59:27
这是网页上的script 我要获取的是00914这个数字 直接使用正则表达式即可
运行结果:
源码:
import re
from bs4 import BeautifulSoup
from urllib.request import urlopen
url = "你要解析的网页URL"
html = urlopen(url).read()
soup = BeautifulSoup(html,"html.parser")
titles = soup.select("body script") # CSS 选择器
i = 1
for title in titles:
if i == 3:
#print(title.get_text())# 标签体、标签属性
str=title.get_text()
break
if i == 2:
i = 3
if i == 1:
i = 2 print(str)
str1 = "\"\"\""+"<script>"+str+"</script>"+"\"\"\""
soup = BeautifulSoup(str1, "html.parser")
pattern = re.compile(r"var _url = '(.*?)';$", re.MULTILINE | re.DOTALL)
script = soup.find("script", text=pattern)
#print (pattern.search(script.text).string)
s = pattern.search(script.text).string
print (s.split('\'')[11])
最新文章
- linux 驱动学习笔记05--文件系统与设备文件系统
- Nexus Repository Manager 3.0 发布
- Java mysql 日期相关
- Mac开发环境搭建
- C++语法疑点
- Java实现Qt的SIGNAL-SLOT机制
- java Socket使用详细解释
- Spring五个事务隔离级别和七个事务传播行为
- python基础之字典、赋值补充
- python(字符串操作)
- 使用 jquery.wordexport.js导出的Word排版
- HTML&;javaSkcript&;CSS&;jQuery&;ajax(11)
- iOS 拨打电话三种方式
- 详解php中serialize()和unserialize()函数
- dedecms前端无法调用自定义变量怎么解决
- 【sping揭秘】2、关于spring配置文件
- Android设计 - 图标设计概述(Iconography)
- Android SDK无法更新的问题解决办法
- IOS-网络(网页开发-UIWebView,HTML,CSS,JavaScript,OC和JS代码互调)
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 7 Regularization 正则化
热门文章
- 腾讯云短信服务+Node.js给手机发送验证码
- CF1195E OpenStreetMap
- 【2019.8.6 慈溪模拟赛 T3】集合(set)(线段树上DP)
- bootstrap-editable 中关于onEditableSave 回调
- maven pom项目的dependencies转gradle格式
- 匿名函数和for_each用法
- Appium swipe实现屏幕滑动
- git 给分支添加描述 管理分支实用方法
- 读取指定页面中的超链接-Python 3.7
- Unsupervised Attention-guided Image-to-Image Translation