黑松山资源网 Design By www.paidiu.com

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

> import webbrowser 
> webbrowser.open('http://i.firefoxchina.cn/"htmlcode">
#! 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/"_blank" href="https://www.jb51.net/article/143370.htm">这里;不清楚.join()用法的,请参考这里。sys.argv是字符串的列表,所以将它传递给join()方法返回一个字符串。

好了,现在选中'天安门广场'这几个字并复制,然后到桌面双击你的程序。当然你也可以在命令行找到你的程序,然后输入地点。

二、用requests模块从Web下载文件:requests模块不是Python自带的,通过命令行运行pip install request安装。没翻墙是很难安装成功的,手动安装可以参考这里。

> import requests 
> res = requests.get('http://i.firefoxchina.cn/"htmlcode">
import requests 
res = requests.get('http://i.firefoxchina.cn/"htmlcode">
> 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): #<span class="fontstyle0"><span class="fontstyle0">iter_content()</span><span class="fontstyle1">方法在循环的每次迭代中返回一段</span><span class="fontstyle0">bytes</span><span class="fontstyle1">数据</span><span class="fontstyle1">类型的内容,你需要指定其包含的字节数</span></span> 
 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/"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 <string>. 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) 
<class 'bs4.BeautifulSoup'> 

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

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

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

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

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

soup.select('div span'):所有在<div>元素之内的<span>元素;

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

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

想查看更多的解析器,请参看这里。

> import requests, bs4 
> res = requests.get('http://i.firefoxchina.cn/"css/mozMainStyle-min.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>, <link href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" moz-skin" rel="stylesheet" type="text/css"/>, <link href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" moz-dir" rel="stylesheet" type="text/css"/>, <link href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" moz-ver" rel="stylesheet" type="text/css"/>] 
> type(link) 
<class 'list'> 
> len(link) 
4 
> type(link[0]) 
<class 'bs4.element.Tag'> 
> link[0] 
<link href="css/mozMainStyle-min.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> 
> link[0].attrs 
{'rel': ['stylesheet'], 'type': 'text/css', 'href': 'css/mozMainStyle-min.css"htmlcode">
> link[0].get('href') 
'css/mozMainStyle-min.css?v=20170705 

上面这些方法也算是对“网络爬虫”的一些初探。

黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?