使用BeautifulSoup轻松获取url及其内容

简介:

1、环境:系统Win7 x64,Python 2.7。

2、示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#encoding: utf-8
#author: walker
#date: 2014-11-26
#summary: 使用BeautifulSoup获取url及其内容
 
import  sys, re, requests, urllib
from  bs4  import  BeautifulSoup
 
reload (sys)   
sys.setdefaultencoding( 'utf8' )  
 
#给定关键词,获取百度搜索的结果   
def  GetList(keyword):
     keyword  =  unicode (keyword,  'gb18030' )
     dic  =  { 'wd' : keyword}
     urlwd  =  urllib.urlencode(dic)
     print (urlwd)
     sn  =  requests.Session()
     url  =  'http://www.baidu.com/s?ie=utf-8&csq=1&pstg=22&mod=2&isbd=1&cqid=9c0f47b700036f17&istc=8560&ver=0ApvSgUI_ODaje7cp4DVye9X2LZqWiCPEIS&chk=54753dd5&isid=BD651248E4C31919&'
     url  + =  urlwd
     url  + =  '&ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&rsv_pq=b05765d70003b6c0&rsv_t=ce54Z5LOdER%2Fagxs%2FORKVsCT6cE0zvMTaYpqpgprhExMhsqDACiVefXOze4&_ck=145469.1.129.57.22.735.37'
     =  sn.get(url = url)
     soup  =  BeautifulSoup(r.content)        #r.text很可能中文乱码
     rtn  =  soup.find( 'div' , id = 'content_left' ).find_all(name = 'a' ,href = re. compile ( 'baidu.com' ))
     for  item  in  rtn:
         print (item.getText().encode( 'gb18030' ))
         print (item[ 'href' ])
         
if  __name__  = =  '__main__' :
     keyword  =  '正则表达式'
     GetList(keyword)

3、运行结果截图:

wKiom1R1R6qwvklLAAX4LcZ3AkI940.jpg


相关阅读:

1、bs4官方文档 。

2、readthedocs

3、Windows下安装失败

4、lxml官网

5、BeautifulSoup深度优先遍历:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#descendants

6、BeautifulSoup广度优先遍历:How to do a Breadth First Search easily with beautiful soup?


*** walker * 2014-11-26 ***


本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1582703如需转载请自行联系原作者

RQSLT
相关文章
|
9天前
|
数据采集 开发者 Python
使用urllib和BeautifulSoup解析网页中的视频链接
使用urllib和BeautifulSoup解析网页中的视频链接
|
3月前
|
XML JavaScript 数据处理
BeautifulSoup和Cheerio库:解析QQ音频文件的完整教程
BeautifulSoup和Cheerio库:解析QQ音频文件的完整教程
|
9月前
|
数据采集 SQL 移动开发
【Python爬虫】Beautifulsoup4中find_all函数
【Python爬虫】Beautifulsoup4中find_all函数
|
5月前
|
XML JSON API
【 ⑨】jsonpath和BeautifulSoup库概述及其对比
【 ⑨】jsonpath和BeautifulSoup库概述及其对比
57 0
|
6月前
|
机器学习/深度学习 数据采集 XML
使用BeautifulSoup解析豆瓣网站的HTML内容并查找图片链接
使用BeautifulSoup解析豆瓣网站的HTML内容并查找图片链接
|
7月前
|
XML C语言 数据格式
七、使用BeautifulSoup4解析HTML实战(一)
七、使用BeautifulSoup4解析HTML实战(一)
|
8月前
|
XML 存储 JavaScript
2022-11-20-使用BeatuifulSoup进行页面内容的获取
2022-11-20-使用BeatuifulSoup进行页面内容的获取
32 0
Beauiful Soup
Beautiful Soup的简单使用
|
XML 数据格式 Python
BeautifulSoup文档1-简介、安装和使用
BeautifulSoup文档1-简介、安装和使用
80 0
How to get text and corresponding tag with BeautifulSoup?
How to get text and corresponding tag with BeautifulSoup?
103 0