menu

arrow_back How can I index 1, 2, etc. to a link?

by
1 vote
Is there some way to output each link separately with an index, or some other way?
import requests
from bs4 import BeautifulSoup

url = requests.get(f"https://all-avito.ru/ru/phone/89039123444")
soup = BeautifulSoup(url.content, 'lxml')

for link in soup.find_all('a'):
keys = ["ru/item/"]
for key in keys:
url = link.get("href")
url = f'https://all-avito.ru/{url}'
if key in str(url):
print(url)

3 Comments

Maybe it's because it's nighttime, but it's hard to understand you.

From the code, I see that you are trying to parse Avito and trying to get all the tags a

Questions arise.

1. What result do you want to get?
2. What is your task? Ч
3. what exactly do you mean by an index?
4. Do you mean the array index or something else?
000pssp , So you already output separately to stdout XD
Searching on search engines ,
I want to get each link separately.
That is, I need to get each link for their subsequent parsing.
By index I mean the output of each link separately from the other.

1 Answer

by
0 votes
If I understand you correctly, you need to generate an array for later use?

It goes something like this:
import requests
from bs4 import BeautifulSoup


def get_urls(url: str) -> list:
url = requests.get(url)
soup = BeautifulSoup(url.content, 'lxml')
keys = ["ru/item/"]

urls = [
f"https://all-avito.ru/{url}"
for url in map(lambda x: x.get('href'), soup.find_all('a'))
for key in keys if key in str(url)
]

return urls


print(get_urls("https://all-avito.ru/ru/phone/89039123444"))