Basic example of using requests and lxml to scrape some data

suggest change
r = requests.get("https://httpbin.org")
html_source = r.text
root_element = lxml.html.fromstring(html_source)

Note

root_element.xpath() gives a list of results.

XPath specifies a path to the element we want.

page_title = root_element.xpath('/html/head/title/text()')[0]
print(page_title)
if __name__ == '__main__':
    main()

requests and lxml are not imported in these snippets. main is not defined on this page, so that call raises NameError. This sample was not run.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents