'im trying to fetch available urls in webpage but the output is like plain text
import requests
from bs4 import BeautifulSoup
url = 'https://www.worldometers.info/world-population/population-by-country/'
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'html.parser')
urls = []
for link in soup.find_all('a'):
urls = link.get('href')
print(urls)
the URL to be printed as "https://www.worldometers.info/world-population/china-population/"
but is just printed as "world-population/china-population"
followed by I need to fetch one particular table from each URL fetched
Solution 1:[1]
That's relative url so you have to make them absolute urls as follows
import requests
from bs4 import BeautifulSoup
url = 'https://www.worldometers.info/world-population/population-by-country/'
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'html.parser')
urls = []
for link in soup.find_all('a'):
urls = link.get('href')
full_url='https://www.worldometers.info'+urls
print(full_url)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | F.Hoque |
