本文實(shí)例講述了Python抓取百度查詢結(jié)果的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
#win python 2.7.ximport re,sys,urllib,codecsxh = urllib.urlopen("http://www.baidu.com/s?q1=123&rn=100").read().decode('utf-8') rc = re.compile(r'<td class=f><h3 class="t"><a.*?>(?P<first>.*?)</h3>',re.I)match = rc.finditer(xh)rcr = re.compile(r'<[^>]+>',re.I)f = codecs.open("xiaohei.txt", "w", "utf-8")for i in rc.finditer(xh): ss = i.group(0) s1 = rcr.sub('',ss) print (s1) f.write(s1)f.close()希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選