Selenium2+python Automation 37-Crawl page source code (PAGE_SOURCE) "Reprint"

Source: Internet
Author: User

Objective

Sometimes it is not easy to find an element on the page by the attribute of the element, and you can crawl the information from the source. Selenium the Page_source method can get to the page source code.

Selenium of the Page_source method is rarely used, small make a recent look API accidentally found this method, so the whim, here combined with the Python re module with regular expression crawled out of all the URL address on the page, can bulk request page URL address, See if there are 404 and other anomalies

First, Page_source

1.selenium Page_source method can be directly returned to the page source code

2. Re-assign the value and print it out

Second, re non-greedy mode

1. You need to import the RE module here

2. Regular matching with re: non-greedy mode

The 3.findall method returns a list collection

4. After matching found some not URL links, you can delete the selected

Third, delete the URL address.

1. Add an If statement to determine that ' http ' in the URL description is the normal URL address

2. Put all the URL addresses in a collection, that's what we want.

Iv. Reference Code

# Coding:utf-8
From selenium import Webdriver
Import re
Driver = Webdriver. Firefox ()
Driver.get ("http://www.cnblogs.com/yoyoketang/")
page = Driver.page_source
# print Page
# "Non-greedy match, re." S ('. ') Match characters, including line breaks) "
Url_list = Re.findall (' href=\ ' (. *?) \ "', page, re. S
Url_all = []
For URL in url_list:
If "http" in URL:
Print URL
Url_all.append (URL)
# The final URL collection
Print Url_all

Selenium2+python Automation 37-Crawl page source code (PAGE_SOURCE) "Reprint"

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.