Python抓取百度查询结果的方法

yipeiwu_com6年前Python爬虫

本文实例讲述了Python抓取百度查询结果的方法。分享给大家供大家参考。具体实现方法如下:

#win python 2.7.x
import re,sys,urllib,codecs
xh = 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程序设计有所帮助。

相关文章

requests和lxml实现爬虫的方法

如下所示: # requests模块来请求页面 # lxml模块的html构建selector选择器(格式化响应response) # from lxml import html # i...

Scrapy框架爬取Boss直聘网Python职位信息的源码

Scrapy框架爬取Boss直聘网Python职位信息的源码

分析 使用CrawlSpider结合LinkExtractor和Rule爬取网页信息 LinkExtractor用于定义链接提取规则,一般使用allow参数即可 LinkExtrac...

python爬虫 execjs安装配置及使用

模块安装 参考官方文档安装 pip install PyExecJS 配置 该模块需要JS运行时环境 以下JS runtime经过官方测试认可,建议采用 PyV8:一...

Python 多线程抓取图片效率对比

目的: 是学习python 多线程的工作原理,及通过抓取400张图片这种IO密集型应用来查看多线程效率对比 import requests import urlparse imp...

python可视化爬虫界面之天气查询

python可视化爬虫界面之天气查询

执行效果如下: from tkinter import * import urllib.request import gzip import json from tkinter...