Hướng dẫn how do you split a link in python? - làm thế nào để bạn tách một liên kết trong python?

1

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi mới đến Python, học những điều cơ bản.

Truy vấn của tôi: Tôi có nhiều trang được truy cập dưới dạng yêu cầu từ tệp nhật ký như bên dưới, I have multiple pages accessed as a request from a log file like the below,

"GET /img/home/search-user-ico.jpg HTTP/1.1"  
"GET /SpellCheck/am.tlx HTTP/1.1"
"GET /img/plan-comp-nav.jpg HTTP/1.1" 
"GET /ie6.css HTTP/1.1"
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1"
"GET /SpellCheck/am100k2.clx HTTP/1.1" 
"GET /SpellCheck/am.tlx HTTP/1.1" 

Câu hỏi của tôi là tôi chỉ muốn phần tệp từ trang, ví dụ, chúng ta hãy coi

parts = line.split(" ")
0 là một trang sau đó từ những điều trên tôi muốn chia
parts = line.split(" ")
1,
parts = line.split(" ")
2.
parts = line.split(" ")
1
,
parts = line.split(" ")
2
.

Vì vậy, các chuyên gia xin vui lòng giúp tôi viết kịch bản Python để chia ra.

Hướng dẫn how do you split a link in python? - làm thế nào để bạn tách một liên kết trong python?

hỏi ngày 18 tháng 4 năm 2011 lúc 8:53Apr 18, 2011 at 8:53

2

Giả sử rằng bạn không có khoảng trống trong tên tệp và bạn không muốn "HTTP" ở cuối.

Bạn có thể chia dòng theo không gian.

parts = line.split(" ")

và sau đó sử dụng mô -đun

parts = line.split(" ")
3 để lấy tên tệp từ đường dẫn.

filename = os.path.basename(parts[1])

Ví dụ.

>>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
>>> parts = line.split(" ")
>>> parts[1]
'/img/home/search-user-ico.jpg'
>>> os.path.basename(parts[1])
'search-user-ico.jpg'

Đã trả lời ngày 18 tháng 4 năm 2011 lúc 9:03Apr 18, 2011 at 9:03

Stephen Paulgerstephen PaulgerStephen Paulger

5.0833 Huy hiệu vàng25 Huy hiệu bạc44 Huy hiệu đồng3 gold badges25 silver badges44 bronze badges

2

data = [
"GET /img/home/search-user-ico.jpg HTTP/1.1",
"GET /SpellCheck/am.tlx HTTP/1.1",
"GET /img/plan-comp-nav.jpg HTTP/1.1" ,
"GET /ie6.css HTTP/1.1",
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
"GET /SpellCheck/am100k2.clx HTTP/1.1" ,
"GET /SpellCheck/am.tlx HTTP/1.1" 
]

for url in data:
    print url.split(' ')[1].split('/')[-2]

Đã trả lời ngày 18 tháng 4 năm 2011 lúc 9:33Apr 18, 2011 at 9:33

1

data = [
"GET /img/home/search-user-ico.jpg HTTP/1.1",
"GET /SpellCheck/am.tlx HTTP/1.1",
"GET /img/plan-comp-nav.jpg HTTP/1.1" ,
"GET /ie6.css HTTP/1.1",
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
"GET /SpellCheck/am100k2.clx HTTP/1.1" ,
"GET /SpellCheck/am.tlx HTTP/1.1" 
]

for url in data:
    print url.split(' ')[1].split('/')[-1]

Đã trả lời ngày 18 tháng 4 năm 2011 lúc 9:05Apr 18, 2011 at 9:05

AchimachimAchim

15.1K15 Huy hiệu vàng75 Huy hiệu bạc139 Huy hiệu đồng15 gold badges75 silver badges139 bronze badges

Nếu định dạng của các liên kết của bạn là tương tự. Một giải pháp khác sẽ là:

request = "GET /img/home/search-user-ico.jpg HTTP/1.1"
parts = request.split("/")
parts[-2] //returns search-user-ico.jpg HTTP

Đã trả lời ngày 18 tháng 4 năm 2011 lúc 9:11Apr 18, 2011 at 9:11

Omerkirkomerkirkomerkirk

2.5371 Huy hiệu vàng17 Huy hiệu bạc9 Huy hiệu đồng1 gold badge17 silver badges9 bronze badges

Làm thế nào để bạn chia một chuỗi theo URL trong Python?

Phương pháp số 1: Sử dụng Split () 'và trả về phần đầu tiên của Split cho kết quả.

Làm thế nào để bạn cắt một url trong python?

  • Phương thức phụ () để xóa URL khỏi văn bản, ví dụ: Kết quả = Re. sub (r'http \ s+',' ', my_string). Ở đó. Phương thức Sub () sẽ xóa bất kỳ URL nào khỏi chuỗi bằng cách thay thế chúng bằng các chuỗi trống.
  • Xem thảo luận
  • Làm thế nào để bạn chia một chuỗi theo URL trong Python?

    Phương pháp số 1: Sử dụng Split () 'và trả về phần đầu tiên của Split cho kết quả.

    Làm thế nào để bạn cắt một url trong python?

    Phương thức phụ () để xóa URL khỏi văn bản, ví dụ: Kết quả = Re. sub (r'http \ s+',' ', my_string). Ở đó. Phương thức Sub () sẽ xóa bất kỳ URL nào khỏi chuỗi bằng cách thay thế chúng bằng các chuỗi trống.
    Method #1 : Using split() 
    This is one of the way in which we can solve this problem. We split by ‘?’ and return the first part of split for result.
     

    Python3

    parts = line.split(" ")
    
    4
    parts = line.split(" ")
    
    5
    parts = line.split(" ")
    
    6

    parts = line.split(" ")
    
    7
    parts = line.split(" ")
    
    8
    parts = line.split(" ")
    
    9
    filename = os.path.basename(parts[1])
    
    0
    filename = os.path.basename(parts[1])
    
    1
    filename = os.path.basename(parts[1])
    
    2

    filename = os.path.basename(parts[1])
    
    3
    parts = line.split(" ")
    
    5
    filename = os.path.basename(parts[1])
    
    5
    filename = os.path.basename(parts[1])
    
    6
    filename = os.path.basename(parts[1])
    
    7
    filename = os.path.basename(parts[1])
    
    8
    filename = os.path.basename(parts[1])
    
    9

    parts = line.split(" ")
    
    7
    parts = line.split(" ")
    
    8
    >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
    >>> parts = line.split(" ")
    >>> parts[1]
    '/img/home/search-user-ico.jpg'
    >>> os.path.basename(parts[1])
    'search-user-ico.jpg'
    
    2
    filename = os.path.basename(parts[1])
    
    0
    >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
    >>> parts = line.split(" ")
    >>> parts[1]
    '/img/home/search-user-ico.jpg'
    >>> os.path.basename(parts[1])
    'search-user-ico.jpg'
    
    4

    Đầu ra: & nbsp;

    The original string is : www.geeksforgeeks.org?is=best
    The base URL is : www.geeksforgeeks.org

    Độ phức tạp về thời gian: O (n) -> (Chức năng phân chia)O(n) -> (split function)

    Không gian phụ trợ: O (n)O(n)

    & nbsp; Phương thức #2: Sử dụng rfind () & nbsp; Đây là một cách khác để chúng ta cần thực hiện nhiệm vụ này. Trong đó, chúng tôi tìm thấy sự xuất hiện đầu tiên của ‘? Từ bên phải và cắt chuỗi. & Nbsp;
    Method #2 : Using rfind() 
    This is another way in which we need to perform this task. In this, we find the first occurrence of ‘?’ from right and slice the string.
     

    Python3

    parts = line.split(" ")
    
    4
    parts = line.split(" ")
    
    5
    parts = line.split(" ")
    
    6

    parts = line.split(" ")
    
    7
    parts = line.split(" ")
    
    8
    parts = line.split(" ")
    
    9
    filename = os.path.basename(parts[1])
    
    0
    filename = os.path.basename(parts[1])
    
    1
    filename = os.path.basename(parts[1])
    
    2

    filename = os.path.basename(parts[1])
    
    3
    parts = line.split(" ")
    
    5
    data = [
    "GET /img/home/search-user-ico.jpg HTTP/1.1",
    "GET /SpellCheck/am.tlx HTTP/1.1",
    "GET /img/plan-comp-nav.jpg HTTP/1.1" ,
    "GET /ie6.css HTTP/1.1",
    "GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
    "GET /SpellCheck/am100k2.clx HTTP/1.1" ,
    "GET /SpellCheck/am.tlx HTTP/1.1" 
    ]
    
    for url in data:
        print url.split(' ')[1].split('/')[-2]
    
    6
    filename = os.path.basename(parts[1])
    
    6
    data = [
    "GET /img/home/search-user-ico.jpg HTTP/1.1",
    "GET /SpellCheck/am.tlx HTTP/1.1",
    "GET /img/plan-comp-nav.jpg HTTP/1.1" ,
    "GET /ie6.css HTTP/1.1",
    "GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
    "GET /SpellCheck/am100k2.clx HTTP/1.1" ,
    "GET /SpellCheck/am.tlx HTTP/1.1" 
    ]
    
    for url in data:
        print url.split(' ')[1].split('/')[-2]
    
    8

    parts = line.split(" ")
    
    7
    parts = line.split(" ")
    
    8
    >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
    >>> parts = line.split(" ")
    >>> parts[1]
    '/img/home/search-user-ico.jpg'
    >>> os.path.basename(parts[1])
    'search-user-ico.jpg'
    
    2
    filename = os.path.basename(parts[1])
    
    0
    >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
    >>> parts = line.split(" ")
    >>> parts[1]
    '/img/home/search-user-ico.jpg'
    >>> os.path.basename(parts[1])
    'search-user-ico.jpg'
    
    4

    Đầu ra: & nbsp;

    The original string is : www.geeksforgeeks.org?is=best
    The base URL is : www.geeksforgeeks.org

    Độ phức tạp về thời gian: O (n) -> (Chức năng phân chia)O(n)

    Không gian phụ trợ: O (n)O(n)

    & nbsp; Phương thức #2: Sử dụng rfind () & nbsp; Đây là một cách khác để chúng ta cần thực hiện nhiệm vụ này. Trong đó, chúng tôi tìm thấy sự xuất hiện đầu tiên của ‘? Từ bên phải và cắt chuỗi. & Nbsp;Using index().Finding index of ‘?’ and then used string slicing

    Python3

    parts = line.split(" ")
    
    4
    parts = line.split(" ")
    
    5
    parts = line.split(" ")
    
    6

    parts = line.split(" ")
    
    7
    parts = line.split(" ")
    
    8
    parts = line.split(" ")
    
    9
    filename = os.path.basename(parts[1])
    
    0
    filename = os.path.basename(parts[1])
    
    1
    filename = os.path.basename(parts[1])
    
    2

    filename = os.path.basename(parts[1])
    
    3
    parts = line.split(" ")
    
    5
    data = [
    "GET /img/home/search-user-ico.jpg HTTP/1.1",
    "GET /SpellCheck/am.tlx HTTP/1.1",
    "GET /img/plan-comp-nav.jpg HTTP/1.1" ,
    "GET /ie6.css HTTP/1.1",
    "GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
    "GET /SpellCheck/am100k2.clx HTTP/1.1" ,
    "GET /SpellCheck/am.tlx HTTP/1.1" 
    ]
    
    for url in data:
        print url.split(' ')[1].split('/')[-2]
    
    6
    filename = os.path.basename(parts[1])
    
    6
    data = [
    "GET /img/home/search-user-ico.jpg HTTP/1.1",
    "GET /SpellCheck/am.tlx HTTP/1.1",
    "GET /img/plan-comp-nav.jpg HTTP/1.1" ,
    "GET /ie6.css HTTP/1.1",
    "GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
    "GET /SpellCheck/am100k2.clx HTTP/1.1" ,
    "GET /SpellCheck/am.tlx HTTP/1.1" 
    ]
    
    for url in data:
        print url.split(' ')[1].split('/')[-2]
    
    8

    parts = line.split(" ")
    
    7
    parts = line.split(" ")
    
    8
    >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
    >>> parts = line.split(" ")
    >>> parts[1]
    '/img/home/search-user-ico.jpg'
    >>> os.path.basename(parts[1])
    'search-user-ico.jpg'
    
    2
    filename = os.path.basename(parts[1])
    
    0
    >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
    >>> parts = line.split(" ")
    >>> parts[1]
    '/img/home/search-user-ico.jpg'
    >>> os.path.basename(parts[1])
    'search-user-ico.jpg'
    
    4

    Đầu ra: & nbsp;

    The original string is : www.geeksforgeeks.org?is = best
    The base URL is : www.geeksforgeeks.org

    Độ phức tạp về thời gian: O (n) -> (Chức năng phân chia)O(n)

    Không gian phụ trợ: O (n)O(n)


    URL chia tay làm gì?

    Kiểm tra URL chia giúp bạn: Tạo và chạy nhiều biến thể của trang web của bạn trên các URL khác nhau.Kiểm tra các luồng khác nhau và các thay đổi phức tạp như thiết kế lại hoàn chỉnh trang web của bạn.So sánh các trang web được lưu trữ trên các URL khác nhau.Create and run multiple variations of your website on different URLs. Test different flows and complex changes such as a complete redesign of your website. Compare webpages hosted on different URLs.

    Làm thế nào để bạn chia một con đường trong Python?

    Phương thức Path.Split () trong Python được sử dụng để chia tên đường dẫn thành đầu cặp và đuôi.Ở đây, Tail là thành phần tên đường dẫn cuối cùng và đầu là tất cả mọi thứ dẫn đến điều đó. split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that.

    Làm thế nào để bạn chia một chuỗi theo URL trong Python?

    Phương pháp số 1: Sử dụng Split () 'và trả về phần đầu tiên của Split cho kết quả.Using split() ' and return the first part of split for result.

    Làm thế nào để bạn cắt một url trong python?

    Phương thức phụ () để xóa URL khỏi văn bản, ví dụ:Kết quả = Re.sub (r'http \ s+',' ', my_string).Ở đó.Phương thức Sub () sẽ xóa bất kỳ URL nào khỏi chuỗi bằng cách thay thế chúng bằng các chuỗi trống., e.g. result = re. sub(r'http\S+', '', my_string) . The re. sub() method will remove any URLs from the string by replacing them with empty strings.