How does a regular expression match Chinese characters in a webpage?

Source: Internet
Author: User
Python crawlers need to extract the & lt; span & gt; AppleiPhone5s (A1530) 16 GB golden mobile Unicom 4G mobile phone & lt; span & gt; here, the phone name cannot match exactly because it is used in beginners. * Only AppleiPhone5s can be matched. (A1, please give me some advice! Has been sleepy python crawler, need to extract html Apple iPhone 5S (A1530) 16 GB gold mobile Unicom 4G mobile phone here on the phone name, because beginners can not match completely, which is used. * Can only match the Apple iPhone 5S (A1 please give pointers! It has been stuck for two days! Reply content: Thank you for your invitation.
I 've come to amway xpath again. Give up the regular expression.

//span[@class="pro-title"]/text()
No one uses BeautifulSoup?
(? <=> ).*? (? = <)
If there are many different "<>" pairs in the actual situation, please fill in the content of the forward and backward assertions by yourself. You should check the Chinese character encoding. The webpage code is probably GBK, however, python uses utf8, so it cannot be matched.
I am so sad to see that I am not familiar with regular expressions !!

--------------- Code zone -------------- # coding: utf-8import rex = 'apple iPhone 5S (A1530) 16 GB gold mobile Unicom 4G mobile phone 'xre = R '()(. +) () 'z = re. search (xre, x ). group (2) print z ---------- output zone ----------------------- C: \ Python27 \ python.exe D:/PycharmProjects/crawler/test. pyApple iPhone 5S (A1530) 16 GB gold mobile Unicom 4G mobile phone process has ended, exit code 0
. +/SSU can usually match Chinese in this way,
Find the node by using the prefix or regular expression, and then match it with the character group above.
Assume that there is only one node. The usage is as follows:

import reimport requests as reqfrom bs4 import BeautifulSoupurl = 'xxx'html = req.get(url).textbs = BeautifulSoup(html)span = bs.find_all('span', 'pro-title')'''span = re.findall('[^<]+', html)s = span[0]m = re.findall('[\u4e00-\u9fa5]+', s)'''s = str(span)m = re.findall('[\u4e00-\u9fa5]+', s)print(m)
If pyquery uses jquery, you will fall in love with it and use phpQuery in php. CSS Selector

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.