User Tools

Site Tools


docu:csheet:sysadm:script:python:html_scraping

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
docu:csheet:sysadm:script:python:html_scraping [2022/01/16 02:11]
admin better examples
docu:csheet:sysadm:script:python:html_scraping [2022/01/16 02:13] (current)
admin clearer examples on attributes
Line 21: Line 21:
 response = requests.get('http://example.org') response = requests.get('http://example.org')
 html = BeautifulSoup(response.text, 'html.parser') html = BeautifulSoup(response.text, 'html.parser')
 +
 +# get page title
 +print(html.title)
  
 # select using DOM selector (list of elements) # select using DOM selector (list of elements)
Line 27: Line 30:
 # examples on findings # examples on findings
 if len(elements) > 0: if len(elements) > 0:
-    # get "value" attribute 
-    print(elements[0].get('value')) 
     # get "href" or "src"     # get "href" or "src"
     print(elements[0].get('href'))     print(elements[0].get('href'))
 +    print(elements[0].get('src'))
  
-    # get class+    # or get using dictionary:
     print(elements[0]['class'])     print(elements[0]['class'])
 +    print(elements[0]['style'])
  
     # get text of DOM     # get text of DOM
docu/csheet/sysadm/script/python/html_scraping.1642299083.txt.gz · Last modified: 2022/01/16 02:11 by admin