Hướng dẫn python requests urljoin - python yêu cầu urljoin

Có thể xây dựng URL bằng thư viện yêu cầu cho Python không?

Xây dựng một chuỗi truy vấn được hỗ trợ nhưng những gì về việc xây dựng phần còn lại của URL. Cụ thể tôi quan tâm đến việc thêm vào URL cơ sở với các chuỗi được mã hóa URL:

http: // một số địa chỉ.com/api/othterTerm

Thuật ngữ = 'Đây là một bài kiểm tra'

http: // một số địa chỉ.com/api/this+is+a+test/

Điều này có lẽ là có thể sử dụng urllib nhưng có vẻ như nó sẽ tốt hơn trong các yêu cầu. Tính năng này có tồn tại không? Nếu không có một lý do chính đáng mà nó không nên?

Hướng dẫn python requests urljoin - python yêu cầu urljoin

Alecxe

451K114 Huy hiệu vàng1048 Huy hiệu bạc1169 Huy hiệu đồng114 gold badges1048 silver badges1169 bronze badges

Đã hỏi ngày 16 tháng 5 năm 2014 lúc 2:13May 16, 2014 at 2:13

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
4 về cơ bản là một trình bao bọc thuận tiện xung quanh
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
5 (và 2,3 và các thư viện liên quan khác).

Bạn có thể nhập

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6,
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
7 từ
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
8, nhưng điều này về cơ bản giống như sử dụng chúng trực tiếp từ các mô -đun
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
5 và
for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
0:

>>> from requests.compat import urljoin, quote_plus
>>> url = "http://some-address.com/api/"
>>> term = 'This is a test'
>>> urljoin(url, quote_plus(term))
'http://some-address.com/api/This+is+a+test'

Đã trả lời ngày 16 tháng 5 năm 2014 lúc 2:29May 16, 2014 at 2:29

Hướng dẫn python requests urljoin - python yêu cầu urljoin

Alecxealecxealecxe

451K114 Huy hiệu vàng1048 Huy hiệu bạc1169 Huy hiệu đồng114 gold badges1048 silver badges1169 bronze badges

3

Ngày: 2014-09-22 14:13

Ngày: 2014-09-22 14:13assignee: orsenthilresolution: fixedmessages: + msg227255

url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list

Ngày: 2014-09-22 14:13messages: + msg227254

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

Ngày: 2014-09-22 14:13

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6

Ngày: 2014-09-22 14:13

Tác giả: Martin Panter (Martin.panter) * Jun 8, 2017 at 13:23

the_t_test_1the_t_test_1the_t_test_1the_t_test_1

Ngày: 2015-03-19 07:11 1 gold badge12 silver badges28 bronze badges

1

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
7

Ngày: 2014-09-22 14:13keywords: + patch

Có một số câu hỏi tương tự với điều này trên StackoverFlow nhưng không ai làm chính xác những gì tôi muốn và những nỗ lực khác nhau của tôi dường như thất bại.

Tôi có một danh sách các URL, một số có dấu vết dấu vết, những người khác thì không ... Tôi muốn kiểm tra chúng và thêm một dấu gạch chéo cho những người không.

Trả về cùng một danh sách (hai URL cuối cùng vẫn không có dấu vết dấu vết)

Tại sao nó không hoạt động? Bất kỳ ý tưởng?

cảm ơn :)Jun 8, 2017 at 13:27

Đã hỏi ngày 8 tháng 6 năm 2017 lúc 13:23Jun 8, 2017 at 13:23asongtoruin

1.1231 Huy hiệu vàng12 Huy hiệu bạc28 Huy hiệu đồng1 gold badge12 silver badges28 bronze badges3 gold badges36 silver badges45 bronze badges

1

Lý do tại sao các thay đổi của bạn không hoạt động là khi bạn nói

url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
1

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
8, điều này không chỉnh sửa
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
9 tại chỗ trong danh sách. Nếu bạn muốn làm theo cách này, bạn có thể tốt hơn khi sử dụng
for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
0:
for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list

url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
2

Chúng tôi có thể gọn gàng hơn điều này bằng cách thay đổi Jun 8, 2017 at 13:28

for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
1 thành
for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
2, trong đó đề cập đến nhân vật cuối cùng.

Nhưng trong khi chúng ta ở đó, tại sao không sử dụng

for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
3 và hiểu biết danh sách?
url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
0Jun 8, 2017 at 13:31

Đã trả lời ngày 8 tháng 6 năm 2017 lúc 13:27Jun 8, 2017 at 13:27tell k

asongtoruinasongtoruinasongtoruin2 gold badges6 silver badges18 bronze badges

9.4663 huy hiệu vàng36 Huy hiệu bạc45 Huy hiệu đồng3 gold badges36 silver badges45 bronze badges

Bạn không thay đổi url_list. Để giữ cấu trúc ban đầu của mã của bạn, bạn có thể thử điều này:2014-08-26 17:58 by demian.brecht, last changed 2022-04-11 14:58 by admin. This issue is now closed.
Bạn cũng có thể sử dụng .endswith () như được đề xuất trong một câu trả lời khác:
Đã trả lời ngày 8 tháng 6 năm 2017 lúc 13:28Jun 8, 2017 at 13:28Biến
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
9 của bạn được phân lập từ
for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
5. Bạn có thể thử điều này:
url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
5
Đã trả lời ngày 8 tháng 6 năm 2017 lúc 13:31Jun 8, 2017 at 13:31
Nói với Ktell ktell k 5952 Huy hiệu vàng6 Huy hiệu bạc18 Huy hiệu đồng2 gold badges6 silver badges18 bronze badges, 2014-08-26 18:00
Ngày: 2015-03-19 07:11
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
72014-08-26 17:58 by demian.brecht, last changed 2022-04-11 14:58 by admin. This issue is now closed.
, 2014-09-18 14:50
Lịch sử
Berker.peksag
NgàyNgười sử dụng*

Hoạt động
Args
2022-04-11 & NBSP; 14: 58: 07 quản trị viên, 2014-08-26 18:00*

bộ
GitHub: 66474, 2014-09-18 14:50
2015-04-15 & NBSP; 23: 11: 15 MSG225923 - (Xem)*

Tác giả: Demian Brecht (Demian.brecht) **
Ngày: 2014-08-26 17:58
url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
7
quản trị viên, 2014-08-26 18:00*

bộ
GitHub: 66474, 2014-09-18 14:50
2015-04-15 & NBSP; 23: 11: 15 Người sử dụng*

Hoạt động
Args
url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
8
Người sử dụng*

url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
7
MSG226164 - (Xem)
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
0
Tác giả: Antoine Pitrou (Pitrou) ***

Ngày: 2014-08-31 10:17
________số 8
MSG226168 - (Xem)Tác giả: Stefan Behnel (Scoder) **

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
2
Ngày: 2014-08-31 10:25
url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
8
Tác giả: Antoine Pitrou (Pitrou) ***

Ngày: 2014-08-31 10:17
________số 8
MSG226168 - (Xem)Người sử dụng*

Tác giả: Stefan Behnel (Scoder) **
Ngày: 2014-08-31 10:25
url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list
8
MSG226240 - (Xem)*

Tác giả: Stefan Behnel (Scoder) *
Ngày: 2014-08-31 10:25
Ngày: 2014-09-01 17:52
MSG226249 - (Xem)*Ngày: 2014-09-01 22:41
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
4
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
1
MSG227048 - (Xem)Ngày: 2014-09-18 14:50 MSG227273 - (Xem) Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
assignee: orsenthil
resolution: fixed
messages: + msg227255
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
messages: + msg227254
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
MSG238494 - (Xem) MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) Ngày: 2014-09-22 14:13 Ngày: 2014-09-22 14:13assignee: orsenthilresolution: fixedmessages: + msg227255
Ngày: 2014-09-22 14:13messages: + msg227254MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
MSG238494 - (Xem) MSG238494 - (Xem) MSG227273 - (Xem) Ngày: 2014-09-22 14:13messages: + msg238494
keywords: + patch
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) Ngày: 2014-09-22 14:13