re正则在python爬虫的应用
2018-02-27 16:32
495 查看
爬虫爬多了,肯定会遇上一些需求不是在H5标签里面的东西。这时候,就只能硬着头皮去使用re正则提取东西了。import re
import urllib2
from lxml import etree
ins_url = 'https://www.instagram.com/ahmad_monk/'
id = 22543622
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0"}
Ruery = urllib2.urlopen(urllib2.Request('https://www.instagram.com/static/bundles/ProfilePageContainer.js/8f295f2125e3.js',headers = headers)).read().decode('utf-8')
pattern = re.compile('queryId:"(\w+)",queryParams')
queryId = re.findall(pattern,Ruery)[1]
first = 12
pattern2 = re.compile('"end_cursor":"(.*?)"')
#提取的内容{"count":43295}}],"count":275,"page_info":{"has_next_page":true,"end_cursor":"AQCVTPsiY5qFjN9Usq6x3fLEAjcGoFCv6MelbGha_EEq3_4K6bjqCMqU7rVaJw-XeojaNP2DrkKJ7qcFI65qv3-JB0THTG5b4gg05F5qhTZc4w"}},"saved_media":{"nodes"碰到这种内容,需要用到非贪婪模式 (.*?)
request = urllib2.Request(ins_url)
response = urllib2.urlopen(request)
R = response.read()
after = re.findall(pattern2,R)[0]
print after
这就提取到了需要的字段
import urllib2
from lxml import etree
ins_url = 'https://www.instagram.com/ahmad_monk/'
id = 22543622
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0"}
Ruery = urllib2.urlopen(urllib2.Request('https://www.instagram.com/static/bundles/ProfilePageContainer.js/8f295f2125e3.js',headers = headers)).read().decode('utf-8')
pattern = re.compile('queryId:"(\w+)",queryParams')
queryId = re.findall(pattern,Ruery)[1]
first = 12
pattern2 = re.compile('"end_cursor":"(.*?)"')
#提取的内容{"count":43295}}],"count":275,"page_info":{"has_next_page":true,"end_cursor":"AQCVTPsiY5qFjN9Usq6x3fLEAjcGoFCv6MelbGha_EEq3_4K6bjqCMqU7rVaJw-XeojaNP2DrkKJ7qcFI65qv3-JB0THTG5b4gg05F5qhTZc4w"}},"saved_media":{"nodes"碰到这种内容,需要用到非贪婪模式 (.*?)
request = urllib2.Request(ins_url)
response = urllib2.urlopen(request)
R = response.read()
after = re.findall(pattern2,R)[0]
print after
这就提取到了需要的字段
相关文章推荐
- python3爬虫(二) re模块与正则表达式
- python爬虫之re正则表达式库
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
- Python高级——正则表达式 re模块 2.高级应用
- Python 爬虫小程序(正则表达式的应用)
- python全栈开发-re模块(正则表达式)应用(字符串的处理)
- Python 正则匹配(re)组的应用
- Python开发基础-Day15正则表达式爬虫应用,configparser模块和subprocess模块
- Python3爬虫(2)xpath模块代替re正则模块的使用
- Python爬虫实例(4)-用urllib、re和正则表达式爬取网页图片
- python爬虫学习之re正则表达式
- python基础之正则表达式爬虫应用,configparser模块和subprocess模块
- Python爬虫实践(七):正则表达式(2) re模块的使用
- python3爬虫之入门和正则表达式,获取IP地址, 隐马尔可夫模型及其在分词中的简单应用
- Python爬虫实践(八):正则表达式re模块(2)
- python爬虫入门(1) 基础知识 ; 正则表达式 Re 模块
- python爬虫入门(2)re模块-正则表达式
- 【Python3 爬虫】09_正则表达式(re.math()、re.search()、re.sub()、全局匹配函数)
- python应用regex正则表达式模块re
- python应用regex正则表达式模块re