python如何在解析HTML页面时,对里面的JS代码中的内容进行提取

坚强的豁着 2015-03-09 03:56:31
例如下面是网页中的JS代码,如何把代码中的URL提取出来,网页的URL是:guangzhou.wxcs.cn/nav/navCity?areaCode=440100
function LetterStorage(){
this.letterJson = {

"A" : [ {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://aba.wxcs.cn/&areacode=513200",name:"阿坝"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://akesu.wxcs.cn/&areacode=652900",name:"阿克苏"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://alashan.wxcs.cn/&areacode=152900",name:"阿拉善"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://aletai.wxcs.cn/&areacode=654300",name:"阿勒泰"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://ankang.wxcs.cn/&areacode=610900",name:"安康"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anqing.wxcs.cn/&areacode=340800",name:"安庆"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anshan.wxcs.cn/&areacode=210300",name:"鞍山"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anshun.wxcs.cn/&areacode=520400",name:"安顺"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anyang.wxcs.cn/&areacode=410500",name:"安阳"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://atushi.wxcs.cn/&areacode=653000",name:"阿图什"} ],"B" : [ {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baicheng.wxcs.cn/&areacode=220800",name:"白城"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baise.wxcs.cn/&areacode=451000",name:"百色"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baisha.wxcs.cn/&areacode=469025",name:"白沙"} ,
...全文
2817 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuxi 2015-03-10
  • 打赏
  • 举报
回复
引用 2 楼 dunkdeng 的回复:
[quote=引用 1 楼 wangzuxi 的回复:] 正则匹配 .
用哪个库都可以吗?[/quote] 正则用re库就可以啊。 获取html的话用request、urllib、urllib2这些库都行。
坚强的豁着 2015-03-10
  • 打赏
  • 举报
回复
引用 1 楼 wangzuxi 的回复:
正则匹配 .
用哪个库都可以吗?
GhostFromHeaven 2015-03-10
  • 打赏
  • 举报
回复

import re
pattern = re.compile(r'url=(http.*?)"', re.I | re.M)
js = """function LetterStorage(){
    this.letterJson = {
(
     )  "A" : [ {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://aba.wxcs.cn/&areacode=513200",name:"阿坝"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://akesu.wxcs.cn/&areacode=652900",name:"阿克苏"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://alashan.wxcs.cn/&areacode=152900",name:"阿拉善"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://aletai.wxcs.cn/&areacode=654300",name:"阿勒泰"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://ankang.wxcs.cn/&areacode=610900",name:"安康"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anqing.wxcs.cn/&areacode=340800",name:"安庆"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anshan.wxcs.cn/&areacode=210300",name:"鞍山"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anshun.wxcs.cn/&areacode=520400",name:"安顺"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anyang.wxcs.cn/&areacode=410500",name:"安阳"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://atushi.wxcs.cn/&areacode=653000",name:"阿图什"} ],"B" : [ {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baicheng.wxcs.cn/&areacode=220800",name:"白城"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baise.wxcs.cn/&areacode=451000",name:"百色"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baisha.wxcs.cn/&areacode=469025",name:"白沙"} , 
"""
urls = pattern.findall(js)
for url in urls:
    print url

http://aba.wxcs.cn/&areacode=513200 http://akesu.wxcs.cn/&areacode=652900 http://alashan.wxcs.cn/&areacode=152900 http://aletai.wxcs.cn/&areacode=654300 http://ankang.wxcs.cn/&areacode=610900 http://anqing.wxcs.cn/&areacode=340800 http://anshan.wxcs.cn/&areacode=210300 http://anshun.wxcs.cn/&areacode=520400 http://anyang.wxcs.cn/&areacode=410500 http://atushi.wxcs.cn/&areacode=653000 http://baicheng.wxcs.cn/&areacode=220800 http://baise.wxcs.cn/&areacode=451000 http://baisha.wxcs.cn/&areacode=469025
GhostFromHeaven 2015-03-10
  • 打赏
  • 举报
回复

import re
pattern = re.compile(r'link:"(http.*?)"', re.I | re.M)
js = """function LetterStorage(){
    this.letterJson = {
(
     )  "A" : [ {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://aba.wxcs.cn/&areacode=513200",name:"阿坝"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://akesu.wxcs.cn/&areacode=652900",name:"阿克苏"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://alashan.wxcs.cn/&areacode=152900",name:"阿拉善"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://aletai.wxcs.cn/&areacode=654300",name:"阿勒泰"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://ankang.wxcs.cn/&areacode=610900",name:"安康"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anqing.wxcs.cn/&areacode=340800",name:"安庆"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anshan.wxcs.cn/&areacode=210300",name:"鞍山"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anshun.wxcs.cn/&areacode=520400",name:"安顺"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://anyang.wxcs.cn/&areacode=410500",name:"安阳"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://atushi.wxcs.cn/&areacode=653000",name:"阿图什"} ],"B" : [ {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baicheng.wxcs.cn/&areacode=220800",name:"白城"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baise.wxcs.cn/&areacode=451000",name:"百色"} , {link:"http://guangzhou.wxcs.cn/nav/toCity?url=http://baisha.wxcs.cn/&areacode=469025",name:"白沙"} , 
"""
urls = pattern.findall(js)
for url in urls:
    print url

http://guangzhou.wxcs.cn/nav/toCity?url=http://aba.wxcs.cn/&areacode=513200 http://guangzhou.wxcs.cn/nav/toCity?url=http://akesu.wxcs.cn/&areacode=652900 http://guangzhou.wxcs.cn/nav/toCity?url=http://alashan.wxcs.cn/&areacode=152900 http://guangzhou.wxcs.cn/nav/toCity?url=http://aletai.wxcs.cn/&areacode=654300 http://guangzhou.wxcs.cn/nav/toCity?url=http://ankang.wxcs.cn/&areacode=610900 http://guangzhou.wxcs.cn/nav/toCity?url=http://anqing.wxcs.cn/&areacode=340800 http://guangzhou.wxcs.cn/nav/toCity?url=http://anshan.wxcs.cn/&areacode=210300 http://guangzhou.wxcs.cn/nav/toCity?url=http://anshun.wxcs.cn/&areacode=520400 http://guangzhou.wxcs.cn/nav/toCity?url=http://anyang.wxcs.cn/&areacode=410500 http://guangzhou.wxcs.cn/nav/toCity?url=http://atushi.wxcs.cn/&areacode=653000 http://guangzhou.wxcs.cn/nav/toCity?url=http://baicheng.wxcs.cn/&areacode=220800 http://guangzhou.wxcs.cn/nav/toCity?url=http://baise.wxcs.cn/&areacode=451000 http://guangzhou.wxcs.cn/nav/toCity?url=http://baisha.wxcs.cn/&areacode=469025
zuxi 2015-03-10
  • 打赏
  • 举报
回复
正则匹配 .

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧