源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-09-15 12:57 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例
熟悉Java的jsoup包的话,对于Python的BeautifulSoup库应该很容易上手。
[u]复制代码[/u] 代码如下:
#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
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部