打印第一行,然后赋值给一个变量。

huangapple go评论73阅读模式
英文:

Printing first line then assign to a variable

问题

for a in soup.find_all('a', href=True):
    if "/xxxxxx/xxxxxxxxxxxxx-" in a['href']:
        x = ("https://www.unkown.com" + a['href'])
        print(x[0])
    else:
        pass
        
my output:
https://www.unkown.com/xxxxx/xxxxxxxxxx
https://www.unkown.com/xxxxx/1xxxxxxxxx
https://www.unkown.com/xxxxx/2xxxxxxxxx
https://www.unkown.com/xxxxx/3xxxxxxxxx
英文:
for a in soup.find_all('a', href=True):
    if "/xxxxxx/xxxxxxxxxxxxx-" in a['href']:
        x = ("https://www.unkown.com" + a['href'])
        print(x[0])
    else:
        pass
        
my output:
https://www.unkown.com/xxxxx/xxxxxxxxxx
https://www.unkown.com/xxxxx/1xxxxxxxxx
https://www.unkown.com/xxxxx/2xxxxxxxxx
https://www.unkown.com/xxxxx/3xxxxxxxxx

I want to print the first line by doing print(x[0]) I just print "h" the output is bunch of url's

答案1

得分: 1

如果您的输出 x 是:

https://www.unkown.com/xxxxx/xxxxxxxxxx
https://www.unkown.com/xxxxx/1xxxxxxxxx
https://www.unkown.com/xxxxx/2xxxxxxxxx
https://www.unkown.com/xxxxx/3xxxxxxxxx

您可以通过调用 x.split('\n')[0] 来访问第一行。

英文:

If your output x is :

https://www.unkown.com/xxxxx/xxxxxxxxxx
https://www.unkown.com/xxxxx/1xxxxxxxxx
https://www.unkown.com/xxxxx/2xxxxxxxxx
https://www.unkown.com/xxxxx/3xxxxxxxxx

You can access the first line by calling x.split('\n')[0].

答案2

得分: 0

在Python中,字符串索引(x[0])返回该索引处的字符,而不是列表中的元素。在您的情况下,x是一个字符串,所以x[0]将返回'h',即您的URL的第一个字符。

要仅打印第一个匹配的URL,您可以使用一个布尔标志。以下是一个示例:

first_match_found = False

for a in soup.find_all('a', href=True):
    if "/xxxxxx/xxxxxxxxxxxxx-" in a['href'] and not first_match_found:
        print("https://www.unkown.com" + a['href'])
        first_match_found = True

此脚本将在找到第一个匹配后停止打印URL。

英文:

In Python, string indexing (x[0]) returns the character at that index, not an element from a list. In your case, x is a string, so x[0] will return 'h', the first character of your URL.

To print only the first matching URL, you can use a boolean flag. Here's an example:

first_match_found = False

for a in soup.find_all('a', href=True):
    if "/xxxxxx/xxxxxxxxxxxxx-" in a['href'] and not first_match_found:
        print("https://www.unkown.com" + a['href'])
        first_match_found = True

This script will stop printing URLs after the first match.

huangapple
  • 本文由 发表于 2023年7月10日 17:47:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定