Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

java 爬虫正则的问题

String Match=""(https://www.tianyancha.com/company/d+)" target='_blank'";
Pattern pattern=Pattern.compile(Match);

Headers.put("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36");
Response=HttpGet("https://www.tianyancha.com/search?key=%E5%8C%97%E4%BA%AC%E7%AB%8B%E7%BA%A2%E9%A4%90%E9%A5%AE%E7%AE%A1%E7%90%86%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8",Headers);
Matcher MatcherUrl=pattern.matcher(Response);
if(MatcherUrl.find()) {
    System.out.println("打印匹配数据");
    System.out.println(MatcherUrl.group(0));
  }

结果是

打印匹配数据
"https://www.tianyancha.com/company/4079618929" target='_blank'

我想要的其实就是

https://www.tianyancha.com/company/4079618929

如何不改变Pattern 只匹配其中的部分内容呢?
python 里面只要大括号就能匹配 括号里面的内容


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我感觉你不需要正则表达式,直接调 Response.getURI()就可以了,可以试试,不知道你用的那个框架解析的HTTP的Response

关注我的博客: https://www.epoooll.com/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...