欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例

时间:2021-07-29 07:42:28|栏目:Python代码|点击:

熟悉Java的jsoup包的话,对于Python的BeautifulSoup库应该很容易上手。

复制代码 代码如下:

#coding: utf-8
import sys
import urllib
import urllib2
from BeautifulSoup import BeautifulSoup

question_word = "吃货 程序员"
url = "http://www.baidu.com/s?wd=" + urllib.quote(question_word.decode(sys.stdin.encoding).encode('gbk'))
htmlpage = urllib2.urlopen(url).read()
soup = BeautifulSoup(htmlpage)
print len(soup.findAll("table", {"class": "result"}))
for result_table in soup.findAll("table", {"class": "result"}):
    a_click = result_table.find("a")
    print "-----标题----\n" + a_click.renderContents()#标题
    print "----链接----\n" + str(a_click.get("href"))#链接
    print "----描述----\n" + result_table.find("div", {"class": "c-abstract"}).renderContents()#描述
    print

上一篇:Python实现查询剪贴板自动匹配信息的思路详解

栏    目:Python代码

下一篇:python 接收处理外带的参数方法

本文标题:使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例

本文地址:http://www.codeinn.net/misctech/162361.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有