-2

How to get the line x of the source Code of a Website?

I Need a function like this:

def source_code(URL, line): ...

  • 1
    Open up a http request, parse the response and return line number x? Check out the requests or urllib3 python modules – Alan Kavanagh Aug 22 '17 at 12:45
  • I recommend you take a look -> Beautifulsoup – Tolgahan ÜZÜN Aug 22 '17 at 12:46
  • 1
    *I need a function like this*. Then start your favorite code editor and start writing one. This isn't a code writing service. Make an effort to solve the problem yourself first. If you run into difficulties, you can then explain the problem you've encountered, include the *relevant* portions of your code, and ask a specific question about that code. See [ask]. – Ken White Aug 22 '17 at 12:46

4 Answers4

3

use request module

import requests as req
url = '"http://www.something.com"'
resp = req.get(url)
print(resp.text) # html response
Rithesh B
  • 198
  • 1
  • 9
2

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.

Ajay V
  • 51
  • 3
2

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))
Anonta
  • 2,500
  • 2
  • 15
  • 25
1

Well, you can save the HTML content of a page, like this, and go to line using the file's functions:

    file_awesome = open('saved_html.html', 'r')
    content = file_awesome.readlines()
    print(content[7])
Abe
  • 1,357
  • 13
  • 31