Use the method find_all() to find all hyperlinks in soup, remembering that hyperlinks are defined by the HTML tag <a> but passed to find_all() without angle brackets; store the result in the variable a_tags. The variable a_tags is a results set: your job now is to enumerate over it, using a for loop and to print the actual URLs of the hyperlinks; to do this, for every element link in a_tags, you want to print() link.get('href').
时间: 2024-04-08 09:29:47 浏览: 102
To find all hyperlinks in the soup using the `find_all()` method and print their URLs, you can use the following code snippet:
```python
# Assuming you already have the soup object named "soup"
# Find all hyperlinks in the soup using the find_all() method
a_tags = soup.find_all('a')
# Enumerate over the a_tags using a for loop
for link in a_tags:
# Print the URL of each hyperlink using link.get('href')
print(link.get('href'))
```
In this code, we use the `find_all()` method on the `soup` object to find all occurrences of the HTML tag `<a>` (hyperlinks). The resulting set of elements is stored in the variable `a_tags`. We then use a for loop to iterate over each element in `a_tags`. Within the loop, we use `link.get('href')` to retrieve and print the URL of each hyperlink.
阅读全文