Skip to content
Advertisement

Set URL from list as header for looped items

So my code looks like this (example URLs):

create an array with URLs

urls = [‘https://www.javatpoint.com/what-is-a-webpage’, ‘https://www.javatpoint.com/welding’]

starting the loop

for url in urls:
# Use requests to retrieve data from a given URL

reqs = requests.get(url, headers={"user-agent": user_agent.chrome})

soup = BeautifulSoup(reqs.text, 'lxml')

print("List of all the headings for", (????)
for heading in soup.find_all(["h1", "h2", "h3", "h4", "h5"]):
  print(heading.name + ' ' + heading.text.strip())

The output I’m getting is this (without URL specification for the different headings)

List of all the headings for:
h2 Blog
h1 What is a Webpage
h2 What is a Webpage?
h4 Note: For practice, you can create web pages on your own without the need for a web server, and your browser will display those pages on your machine only.
h4 Note: A web browser can also display other documents such as a PDF document or images, but only an HTML document is referred to as Web page.
h2 Characteristics of a Web Page
h2 Difference between a Webpage and a Website
h4 Note: The terms Webpage and Web page are the same, and both are technically correct. However, most style guides suggest using a Webpage instead of a Web page.
h2 How does a Web Page Work?
h2 Elements of a Webpage
h2 Types of a Web page
h3 Static Webpage
h3 Dynamic Webpage
h4 Note: Scripting languages are programming languages that allow us to write programs in the form of scripts, and these are interpreted, not compiled.
h2 How to Create a Simple Webpage?
h4 Note: It is important to end each tag in html, and put all the tags either in upper case or lower case. However, a lower case is recommended.
h4 Note: This webpage is local to your machine only, and only you can see this on your browser. To view this on the internet, you need to first publish it.
h3 Feedback
h2 Help Others, Please Share
h2 Learn Latest Tutorials
h2 Preparation
h2 Trending Technologies
h2 B.Tech / MCA
h2 Javatpoint Services
h2 Training For College Campus
h3 Learn Tutorials
h3 Our Websites
h3 Our Services
h3 Contact

List of all the headings for:
h2 Blog
h1 Welding
h2 Types of Energy sources of welding
h3 Gas flame welding
h3 Electric arc welding
h3 Electron beam welding
h3 Ultrasound welding
h3 Laser welding
h3 Friction welding
h2 History of welding
h2 Methods of welding
h3 Shielded metal arc welding
h3 Gas metal arc welding
h3 Gas tungsten arc welding
h3 Submerged arc welding
h3 Flux-cored arc welding
h3 Electroslag welding
h3 Electrogas welding
h2 Safety Issues
h2 General Applications of welding
h2 Soldering vs. Brazing vs. Welding
h3 Feedback
h2 Help Others, Please Share
h2 Learn Latest Tutorials
h2 Preparation
h2 Trending Technologies
h2 B.Tech / MCA
h2 Javatpoint Services
h2 Training For College Campus
h3 Learn Tutorials
h3 Our Websites
h3 Our Services
h3 Contact


The output I want (with URL specification for the different headings)

List of all the headings for https://www.javatpoint.com/what-is-a-webpage:
h2 Blog
h1 What is a Webpage
h2 What is a Webpage?
h4 Note: For practice, you can create web pages on your own without the need for a web server, and your browser will display those pages on your machine only.
h4 Note: A web browser can also display other documents such as a PDF document or images, but only an HTML document is referred to as Web page.
h2 Characteristics of a Web Page
h2 Difference between a Webpage and a Website
h4 Note: The terms Webpage and Web page are the same, and both are technically correct. However, most style guides suggest using a Webpage instead of a Web page.
h2 How does a Web Page Work?
h2 Elements of a Webpage
h2 Types of a Web page
h3 Static Webpage
h3 Dynamic Webpage
h4 Note: Scripting languages are programming languages that allow us to write programs in the form of scripts, and these are interpreted, not compiled.
h2 How to Create a Simple Webpage?
h4 Note: It is important to end each tag in html, and put all the tags either in upper case or lower case. However, a lower case is recommended.
h4 Note: This webpage is local to your machine only, and only you can see this on your browser. To view this on the internet, you need to first publish it.
h3 Feedback
h2 Help Others, Please Share
h2 Learn Latest Tutorials
h2 Preparation
h2 Trending Technologies
h2 B.Tech / MCA
h2 Javatpoint Services
h2 Training For College Campus
h3 Learn Tutorials
h3 Our Websites
h3 Our Services
h3 Contact

List of all the headings for https://www.javatpoint.com/welding:
h2 Blog
h1 Welding
h2 Types of Energy sources of welding
h3 Gas flame welding
h3 Electric arc welding
h3 Electron beam welding
h3 Ultrasound welding
h3 Laser welding
h3 Friction welding
h2 History of welding
h2 Methods of welding
h3 Shielded metal arc welding
h3 Gas metal arc welding
h3 Gas tungsten arc welding
h3 Submerged arc welding
h3 Flux-cored arc welding
h3 Electroslag welding
h3 Electrogas welding
h2 Safety Issues
h2 General Applications of welding
h2 Soldering vs. Brazing vs. Welding
h3 Feedback
h2 Help Others, Please Share
h2 Learn Latest Tutorials
h2 Preparation
h2 Trending Technologies
h2 B.Tech / MCA
h2 Javatpoint Services
h2 Training For College Campus
h3 Learn Tutorials
h3 Our Websites
h3 Our Services
h3 Contact


Would this be possible to achieve? If so, thank you for your help!

Advertisement

Answer

print("List of all the headings for %s" % url)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement