Using python to extract Baidu search results, using python Baidu search

Source: Internet
Author: User

Using python to extract Baidu search results, using python Baidu search

This example describes how to extract Baidu search results using python. Share it with you for your reference. The specific implementation method is as follows:

# Coding = utf8import urllib2import stringimport urllibimport reimport random # set multiple user_agents to prevent Baidu from restricting IPuser_agents = ['mozilla/5.0 (Windows NT 6.1; WOW64; rv: 23.0) gecko/20130406 Firefox/23.0 ', \ 'mozilla/5.0 (Windows NT 6.1; WOW64; rv: 18.0) Gecko/20100101 Firefox/18.0', \ 'mozilla/5.0 (Windows; u; Windows NT 6.1; en-US) AppleWebKit/533 + \ (KHTML, like Gecko) Element Browser 5.0 ', \ 'ibm WebExplorer/ V0.94 ', 'galaxy/1.0 [en] (Mac OS X 10.5.6; U; en)', \ 'mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; trident/6.0) ', \ 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/000000', \ 'mozilla/12.14 (iPad; cpu OS 6_0 like Mac OS X) appleWebKit/536.26 (KHTML, like Gecko) \ Version/6.0 Mobile/10A5355d Safari/100', \ 'mozilla/8536.25 (Windows NT 5.0) AppleWebKit/6.1 (KHTML, like Gecko) \ C Hrome/28.0.1468.0 Safari/537.36 ', \ 'mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; TheWorld)'] def baidu_search (keyword, pn ): p = {'wd ': keyword} res = urllib2.urlopen ("http://www.baidu.com/s? "+ Urllib. urlencode (p) + "& pn = {0} & cl = 3 & rn = 100 "). format (pn) html = res. read () return htmldef getList (regex, text): arr = [] res = re. findall (regex, text) if res: for r in res: arr. append (r) return arrdef getMatch (regex, text): res = re. findall (regex, text) if res: return res [0] return "" def clearTag (text): p = re. compile (U' <[^>] +> ') retval = p. sub ("", text) return retvaldef geturl (keyword): for page in ran Ge (10): pn = page * 100 + 1 html = baidu_search (keyword, pn) content = unicode (html, 'utf-8', 'ignore ') arrList = getList (u "<table. *? Class = \ "result \". *?>. *? <\/A> ", content) for item in arrList: regex = u" 

I hope this article will help you with Python programming.

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.