英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论