Ruby-Get product links on the website

Source: Internet
Author: User

The scripts written in Ruby run faster, and you decided to use ruby.

Compared with my e-commerce website, I have the opportunity to use it: Get all URLs on a page.

Save the following script as URLScan. RB. Usage: run the CMD command to enter the script path and run Ruby URLScan. rb url.

In the script, Data. Scan () can be placed into other regular expressions you want to search for on the corresponding page, such as data. Scan (/<a href = "(.*?) ">/), That is, all URLs

Obtain all product links in the following example.

URLScan. Rb

Require 'net/HTTP 'filename = file. new ('C: \ 1.txt ', "W +") if $ * [0] = nilputs "not URL, please for: Ruby URLScan. RB www.gaopeng.com "elseh = net: HTTP. new ($ * [0], 80) resp, Data = H. post ('HTTP: // www.gaopeng.com/', nil) # obtain the tag of the page for obtaining all URLs, for example:/index.html if resp. message = "OK" data. scan (/<a target = "_ blank" href = "(. *?) "/). Each do | x | # Get all URLs marked as href # <a target =" _ blank "href =" http://www.gaopeng.com/deals/beijing/ftuan/1431948203 "> puts xfilename. Puts xendendend

Here, the POST method in HTTP. Rb is used. If an error occurs in the get method, no get () is used ().

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.