Reputation: 1767
I am trying to extract the href's from the web page of an institution. I have to extract the dept codes for further crawling activity. and I have written following code:
import requests
import re
import urllib
from bs4 import BeautifulSoup
codesurl="http://www.iitkgp.ac.in/academics/?page=acadunits"
response = requests.get(codesurl)
# print(response.content)
soup=BeautifulSoup(response.content)
# print(soup.prettify())
p = re.compile('page=acadunits*')
p1 = re.compile('<a href=.*page=acadunits*')
links=soup.find_all("a")
print(links)
for link in links:
# if p1.match(link):
print("%s" %(link))
But I am not getting all the href's for example :
<a href="?page=acadunits&&dept=ME">Mechanical Engineering</a>
<a href="?page=acadunits&&dept=MD">Medical Science & Technology</a>
<a href="?page=acadunits&&dept=MT">Metallurgical & Materials Engineering</a>
and many more Can somebody help me with this.This is first time I am crawling. you can also look at the website.I need to extract dept code from url
dept=ME
dept=MT
dept=MD
My web page contains:
<div class="tab_container">
<div id="tab1" class="tab_content" style="display: block;">
<h3></h3>
<!--Content-->
<img src="./Indian Institute of Technology Kharagpur_files/academicunits.jpg">
<br><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=AE">Aerospace Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=AG">Agricultural & Food Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=AR">Architecture & Regional Planning</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=BT">Biotechnology</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=CH">Chemical Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=CM">Chemistry</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=CE">Civil Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=CS">Computer Science & Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=CR">Cryogenic Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=ED">Center for Educational Technology</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=EE">Electrical Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=EC"> Electronics & Electrical Communication Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=GS">G S Sanyal School of Telecommunications</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=MG">Geology & Geophysics</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=HS">Humanities & Social Sciences</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=IM">Industrial & Systems Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=IT">Information Technology</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=MS">Materials Science</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=MM">Mathematics</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=ME">Mechanical Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=MD">Medical Science & Technology</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=MT">Metallurgical & Materials Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=MI">Mining Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=NA">Ocean Engineering & Naval Architecture</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=N2">Oceans, Rivers, Atmosphere and Land Sciences</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=MP">Physics</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=PK">P K Sinha Centre for Bio Energy</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=RJ">Rajendra Mishra School of Engineering Entrepreneurship</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=RG">Rajiv Gandhi School of Intellectual Property Law</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=ID">Ranbir and Chitra Gupta School of Infrastructure Design and Management</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=RE">Reliability Engineering Centre</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=RT">Rubber Technology Centre</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=RD">Rural Development Centre</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=BS">School of Bioscience</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=ES">School of Energy Science & Engineering</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=EF">School of Environmental Science and Technology</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=NT">School of Nano-Science and Technology</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=WM">School of Water Resources</a><br>
<a href="http://www.iitkgp.ac.in/academics/?page=acadunits&&dept=SM">Vinod Gupta School of Management</a><br>
<br><br>
<!--Content-->
</div>
but when I do :
codesurl="http://www.iitkgp.ac.in/academics/?page=acadunits"
response = requests.get(codesurl)
soup=BeautifulSoup(response.text)
soup does not show these href's can someone suggest how to extract these href tags??
Upvotes: 0
Views: 143
Reputation: 473813
First of all, the department links are loaded dynamically with a GET request to this URL.
Then, the idea would be to find all links where href
attribute value is matching a specific pattern and then use this pattern to extract the department codes. Working code:
import re
import requests
from bs4 import BeautifulSoup
codesurl = "http://www.iitkgp.ac.in/academics/academic.php"
response = requests.get(codesurl)
soup = BeautifulSoup(response.content, "lxml")
pattern = re.compile(r"dept=([A-Z]+)")
links = soup.find_all("a", href=pattern)
for link in links:
print(pattern.search(link["href"]).group(1))
Prints:
AE
AG
AR
...
NT
WM
SM
Upvotes: 1
Reputation: 61225
The best way to do this is using parse_qs
from the urllib.parse
module.
for link in links:
qs = parse_qs(link.get('href'))
print('dept', qs['dept'][0])
or using rpartition
for link in links:
print(link.get('href').rpartition('&&')[-1])
Upvotes: 0