Python daily Practice (2): Find all links in HTML (Xpath, regular two versions)

Source: Internet
Author: User
Tags xpath

To find out the specific content in the Hrml file, you first need to observe what the content is and where it is, so you can find it.

Assume that the HTML file name is: "1.html", the href attribute is all in the a tag.

Regular version:

# Coding:utf-8 Import Rewith Open ('1.html','r') as F:     == Re.findall (R'href= "(. *?)" ' , data)  for inch Result:     Print each

XPath version:

#Coding:utf-8 fromlxmlImportEtreewith Open ('1.html','R') as F:data=f.read () selector=etree. HTML (data) result= Selector.xpath ('//a/@href') foreachinchResult:Printeach

The XPath version is more than the regular version of the page, this HTML file seems to be a bit long posted up when the show 502, solve.

Does it seem that the editor doesn't have a place to upload attachments?

Python daily Practice (2): Find all links in HTML (Xpath, regular two versions)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.