加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 19:58:12 所属栏目: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/

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读