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

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

Printing first line then assign to a variable

问题

  1. for a in soup.find_all('a', href=True):
  2. if "/xxxxxx/xxxxxxxxxxxxx-" in a['href']:
  3. x = ("https://www.unkown.com" + a['href'])
  4. print(x[0])
  5. else:
  6. pass
  7. my output:
  8. https://www.unkown.com/xxxxx/xxxxxxxxxx
  9. https://www.unkown.com/xxxxx/1xxxxxxxxx
  10. https://www.unkown.com/xxxxx/2xxxxxxxxx
  11. https://www.unkown.com/xxxxx/3xxxxxxxxx
英文:
  1. for a in soup.find_all('a', href=True):
  2. if "/xxxxxx/xxxxxxxxxxxxx-" in a['href']:
  3. x = ("https://www.unkown.com" + a['href'])
  4. print(x[0])
  5. else:
  6. pass
  7. my output:
  8. https://www.unkown.com/xxxxx/xxxxxxxxxx
  9. https://www.unkown.com/xxxxx/1xxxxxxxxx
  10. https://www.unkown.com/xxxxx/2xxxxxxxxx
  11. 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 是:

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

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

英文:

If your output x is :

  1. https://www.unkown.com/xxxxx/xxxxxxxxxx
  2. https://www.unkown.com/xxxxx/1xxxxxxxxx
  3. https://www.unkown.com/xxxxx/2xxxxxxxxx
  4. 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,您可以使用一个布尔标志。以下是一个示例:

  1. first_match_found = False
  2. for a in soup.find_all('a', href=True):
  3. if "/xxxxxx/xxxxxxxxxxxxx-" in a['href'] and not first_match_found:
  4. print("https://www.unkown.com" + a['href'])
  5. 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:

  1. first_match_found = False
  2. for a in soup.find_all('a', href=True):
  3. if "/xxxxxx/xxxxxxxxxxxxx-" in a['href'] and not first_match_found:
  4. print("https://www.unkown.com" + a['href'])
  5. 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:

确定