网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

用Python爬取网页数据的方法-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

为忻府等地区用户提供了全套网页设计制作服务,及忻府网站建设行业解决方案。主营业务为网站制作、网站设计、忻府网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

小编给大家分享一下用Python爬取网页数据的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

使用Python爬取网页数据的方法:

一、利用webbrowser.open()打开一个网站:

>>> import webbrowser 
>>> webbrowser.open('http://i.firefoxchina.cn/?from=worldindex') 
True

1.从sys.argv读取命令行参数:打开一个新的文件编辑器窗口,输入下面的代码,将其保存为map.py。

2.读取剪贴板内容:

3.调用webbrowser.open()函数打开外部浏览:

#! python3 
import webbrowser, sys, pyperclip 
if len(sys.argv) > 1: 
 mapAddress = ''.join(sys.argv[1:]) 
else: 
 mapAddress = pyperclip.paste() 
webbrowser.open('http://map.baidu.com/?newmap=1&ie=utf-8&s=s%26wd%3D' + mapAddress

二、用requests模块从Web下载文件:requests模块不是Python自带的,通过命令行运行pip install request安装。

>>> import requests 
>>> res = requests.get('http://i.firefoxchina.cn/?from=worldindex') #向get中传入一个网址 
>>> type(res) #响应对象 
 
>>> print(res.status_code) #响应码 
200
>>> res.text #返回的文本

在下载文件的过程中,用raise_for_status()方法可以确保下载确实成功,然后再让程序继续做其他事情。

import requests 
res = requests.get('http://i.firefoxchina.cn/?from=worldindex') 
try: 
 res.raise_for_status() 
except Exception as exc: 
 print('There was a problem: %s' % (exc))

三、将下载的文件保存到本地:

>>> import requests 
>>> res = requests.get('http://tech.firefox.sina.com/17/0820/10/6DKQALVRW5JHGE1I.html##0-tsina-1-13074-397232819ff9a47a7b7e80a40613cfe1') 
>>> res.raise_for_status() 
>>> file = open('1.txt', 'wb') #以写二进制模式打开文件,目的是保存文本中的“Unicode编码” 
>>> for word in res.iter_content(100000): #iter_content()方法在循环的每次迭代中返回一段bytes数据类型的内容,你需要指定其包含的字节数 
 file.write(word) 
  
  
16997
>>> file.close()

四、用BeautifulSoup模块解析HTML:在命令行中用pip install beautifulsoup4安装它。
1.bs4.BeautifulSoup()函数可以解析HTML网站链接requests.get(),也可以解析本地保存的HTML文件,直接open()一个本地HTML页面。

>>> import requests, bs4 
>>> res = requests.get('http://i.firefoxchina.cn/?from=worldindex') 
>>> res.raise_for_status() 
>>> soup = bs4.BeautifulSoup(res.text) 
  
Warning (from warnings module): 
 File "C:\Users\King\AppData\Local\Programs\Python\Python36-32\lib\site-packages\beautifulsoup4-4.6.0-py3.6.egg\bs4\__init__.py", line 181
 markup_type=markup_type)) 
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently. 
  
The code that caused this warning is on line 1 of the file . To get rid of this warning, change code that looks like this: 
  
 BeautifulSoup(YOUR_MARKUP}) 
  
to this: 
  
 BeautifulSoup(YOUR_MARKUP, "html.parser") 
  
>>> soup = bs4.BeautifulSoup(res.text, 'html.parser') 
>>> type(soup) 

我这里有错误提示,所以加了第二个参数。

>>> import bs4 
>>> html = open('C:\\Users\\King\\Desktop\\1.htm') 
>>> exampleSoup = bs4.BeautifulSoup(html) 
>>> exampleSoup = bs4.BeautifulSoup(html, 'html.parser') 
>>> type(exampleSoup) 

2.用select()方法寻找元素:需传入一个字符串作为CSS“选择器”来取得Web页面相应元素,例如:
soup.select('div'):所有名为

的元素;

soup.select('#author'):带有id属性为author的元素;

soup.select('.notice'):所有使用CSS class属性名为notice的元素;

soup.select('div span'):所有在

元素之内的元素;

soup.select('input[name]'):所有名为并有一个name属性,其值无所谓的元素;

soup.select('input[type="button"]'):所有名为并有一个type属性,其值为button的元素。

>>> import requests, bs4 
>>> res = requests.get('http://i.firefoxchina.cn/?from=worldindex') 
>>> res.raise_for_status() 
>>> soup = bs4.BeautifulSoup(res.text, 'html.parser') 
>>> author = soup.select('#author') 
>>> print(author) 
[] 
>>> type(author) 
 
>>> link = soup.select('link ') 
>>> print(link) 
[] 
>>> type(link) 
 
>>> len(link) 
4
>>> type(link[0]) 
 
>>> link[0] 
 
>>> link[0].attrs 
{'rel': ['stylesheet'], 'type': 'text/css', 'href': 'css/mozMainStyle-min.css?v=20170705'}

 3.通过元素的属性获取数据:接着上面的代码写。

>>> link[0].get('href') 
'css/mozMainStyle-min.css?v=20170705

看完了这篇文章,相信你对用Python爬取网页数据的方法有了一定的了解,想了解更多相关知识,欢迎关注创新互联-成都网站建设公司行业资讯频道,感谢各位的阅读!


分享文章:用Python爬取网页数据的方法-创新互联
文章分享:http://bjjierui.cn/article/pjhop.html