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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例的相关内容吗,在本文为您仔细讲解python爬虫程序获取百度搜索结果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:爬虫程序,百度搜索结果,BeautifulSoup,下面大家一起来学习吧。

熟悉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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们