一个爬虫前的配置及爬虫简示

采用Python3环境,并通过pip装几个包:




大概是我的系统环境变量没有设置好吧,我看网上都是直接pip install xxx,我的还要多加python -m 指令

设置好后,在python文件夹的Scripts里找到jupyter notebook.exe,打开,复制地址换浏览器需要输入服务器密码,按照网上教程我也没有修改成功,不知道原因,待解决吧。

右上角New-python3,开始编写属于你的第一个爬虫吧。


简单爬新浪网:

import requests
res = requests.get('http://mobile.sina.com.cn/')
res.encoding = 'utf-8'
print(res.text)

这里get的原理在  http://study.163.com/my  网络爬虫实战中讲到,选择Chrome浏览器,右键检查,选择Doc,在Preview可以查看内容(需要F5重新爬取),是否爬取到内容


其他小试:

from bs4 import BeautifulSoup
html_sample = '\
<html> \
    <head> \
        <meta charset="utf-8"> \
        <title></title> \
    </head> \
    <body> \
        <h1> This is a test</h1> \
    </body> \
</html>'

soup = BeautifulSoup(html_sample,'html.parser')
header = soup.select('h1')
print(header)
print(header[0].text)

from bs4 import BeautifulSoup

html_sample = '\
<!DOCTYPE html> \
<html> \
    <head> \
        <meta charset="utf-8"> \
        <title></title> \
    </head> \
    <body> \
        <h1 id = "title">C++</h1> \
        <h1 id = "title">Python</h1> \
        <a href="#" class = "link"> This is link2 </a> \
    </body> \
</html> '


soup = BeautifulSoup(html_sample,'html.parser')
alink = soup.select('#title')
print(alink)
print('\n')
for link in alink:
    print(link)
print('\n')
for link in alink:
    print(link.text)

输出:

[<h1 id="title">C++</h1>, <h1 id="title">Python</h1>]


<h1 id="title">C++</h1>
<h1 id="title">Python</h1>


C++
Python


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值