How to get the line x of the source Code of a Website?
I Need a function like this:
def source_code(URL, line): ...
How to get the line x of the source Code of a Website?
I Need a function like this:
def source_code(URL, line): ...
use request module
import requests as req
url = '"http://www.something.com"'
resp = req.get(url)
print(resp.text) # html response
There is a standard library module in python: urllib2, you could also check out python-requests Try the following then:
import urllib2
resp = urllib2.urlopen("The URL of the webpage whose source code you want")
Now go through https://www.crummy.com/software/BeautifulSoup/bs4/doc/ , this is BeautifulSoup, which you can use for parsing. You can just set the condition of which line to retrieve using it.
This should do it
import requests
def source_code(url, line):
# get the page source code and split each line
lines = requests.get(url).text.split('\n')
# page source code had too few lines
if len(lines) < line : return ''
else: return lines[line-1]
print(source_code('somepageurl', 9))