Home  >  Q&A  >  body text

How to use split first in python

from_url_start ='s?q='
            if line.find(from_url_start)!=-1:
                from_url = +line.split(from_url_start)[1].split('&')[0]

I set the starting index to 's?q=', and then

from_url = +line.split(from_url_start)[1].split('&')[0]

Set the string that ends split('&') to be this '&', and then extract the two middle values

The value inside

is

%E6%B0%B4%E6%9E%9C%E5%B9%B2%E8%8C%B6%E5%88%B6%E4%BD%9C

The complete code is

s?q=%E8%8D%92%E9%87%8E%E8%8C%B6%E5%92%8C%E8%8C%B6%E5%9B%AD%E8%8C%B6%E7%9A%84%E5%8C%BA%E5%88%AB&src=result_input&srcg=360aphone&rg=0&_ms=0&log_id=548920"
或者
s?q=%E5%B0%8F%E7%BD%90%E8%8C%B6%E5%8A%A0%E7%9B%9F%E8%B4%B9%E8%A6%81%E5%A4%9A%E5%B0%91%E9%92%B1"

There are two ending identifiers & and "

in the complete code

Is there a way to set one, and only select "this to end the interception area when & does not exist?
What kind of regular rules are needed
If split('&|"') is set like this, it will also Extract unnecessary parameters later
such as

%E8%8D%92%E9%87%8E%E8%8C%B6%E5%92%8C%E8%8C%B6%E5%9B%AD%E8%8C%B6%E7%9A%84%E5%8C%BA%E5%88%AB&src=result_input&srcg=360aphone&rg=0&_ms=0&log_id=548920

He extracted this paragraph

PHP中文网PHP中文网2711 days ago976

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-18 11:01:49

    You can use replace first to replace & with "

    reply
    0
  • Cancelreply