网页爬虫入门--莫烦教程笔记

网页爬虫入门–莫烦教程笔记

教程推荐:

莫烦教程–网页爬虫

崔庆才–Python爬虫学习系列教程

知乎问答中的各种推荐

孔淼–一看就明白的爬虫入门讲解

课程逻辑:

网页爬虫 解析网页 高效爬虫 爬虫高级库

爬虫简介

# 用Python登录网页

from urllib.request import urlopen

# if has Chinese, apply decode()
html = urlopen(
    "https://morvanzhou.github.io/static/scraping/basic-structure.html"
).read().decode('utf-8')
print(html)
<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>Scraping tutorial 1 | 莫烦Python</title>
    <link rel="icon" href="https://morvanzhou.github.io/static/img/description/tab_icon.png">
</head>
<body>
    <h1>爬虫测试1</h1>
    <p>
        这是一个在 <a href="https://morvanzhou.github.io/">莫烦Python</a>
        <a href="https://morvanzhou.github.io/tutorials/scraping">爬虫教程</a> 中的简单测试.
    </p>

</body>
</html>

注意:

我用jupyter notebook做动态交互, 运行时一直报错”ImportError: No module named request”. 但这段代码在IDE上没有出错, 并且这个模块是存在的. 所有我花了一些时间解决这个问题, 在此记录, 以防再次犯错.

  1. 首先分析是否是安装问题, 由于我很久没有用jupyter, 再次使用时忘了anaconda自带jupyter,所有用pip和pip3都分别安装了一次. 出问题后又用uninstall卸载, 但是问题没有解决, 依然报错. 并且发现anaconda里有jupyter.

  2. google以及stackoverflow上找办法, 无果, 感觉问题还是出在Python版本上. 于是修改了配置文件, 还是出错.

  3. 分析了一下原因, 觉得问题应该出在anaconda自带jupyer上, 其默认的环境不是我正在使用的环境python3.5, 一番周折之后找到解决方案. 原来jupyter的ipykernel是使用一个叫kernel.json的文件管理的, 所以直接为我想要的环境安装ipykernel包:

$ conda install -n python35 ipykernel 
$ python -m ipykernel install --user #激活这个环境
# 匹配网页内容,初级网页匹配使用正则,繁琐匹配推荐使用BeautifulSoup

# 想要找到title的话
import re
res=re.findall(r'<title>(.+?)</title>',html)
print('\nPage title is:',res[0])

# 想要找到中间的那个段落
res=re.findall(r'<p>(.+?)</p>',html,flags=re.DOTALL) #re.DOTALL if multil line
print('\nPage paragraph is:',res[0])

# 找一找所有的链接
res=re.findall(r'href=(.+?)',html)
print('\nAll links:',res)
Page title is: Scraping tutorial 1 | 莫烦Python

Page paragraph is: 
        这是一个在 <a href="https://morvanzhou.github.io/">莫烦Python</a>
        <a href="https://morvanzhou.github.io/tutorials/scraping">爬虫教程</a> 中的简单测试.


All links: ['"', '"', '"']

BeautifulSoup解析网页

爬网页流程:
1. 选择要爬的网址(url)
2. 使用Python登录上这个网址(urlopen等)
3. 读取网页信息(read()出来)
4. 将读取的信息放入BeautifulSoup
5. 使用BeautifulSoup选取tag信息等(代替正则表达式)

安装BeautifulSoup

$ pip install beautifulsoup4 # Python 2+ 
$ pip3 install beautifulsoup4 # Python 3+ 
$ conda install beautifulsoup4 # anaconda

BeautifulSoup 4.2.0文档

BeautifulSoup解析网页: 基础

# 按常规读取网页
from bs4 import BeautifulSoup
from urllib.request import urlopen

#if has Chinese, apple decode()
html = urlopen(
    "https://morvanzhou.github.io/static/scraping/basic-structure.html"
).read().decode('utf-8')
print(html)

# 将网页信息加载进BeautifulSoup,以lxml的形式
soup=BeautifulSoup(html,features='lxml')
print(soup.h1)
print('\n',soup.p)

# 用find_all找到所有选项,但真正link在<a href="link">里面
# 可以看做是<a>的一个属性,可以用字典形式的key来读取
all_href=soup.find_all('a')
all_href=[l["href"] for l in all_href]
print('\n',all_href)
<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>Scraping tutorial 1 | 莫烦Python</title>
    <link rel="icon" href="https://morvanzhou.github.io/static/img/description/tab_icon.png">
</head>
<body>
    <h1>爬虫测试1</h1>
    <p>
        这是一个在 <a href="https://morvanzhou.github.io/">莫烦Python</a>
        <a href="https://morvanzhou.github.io/tutorials/scraping">爬虫教程</a> 中的简单测试.
    </p>

</body>
</html>
<h1>爬虫测试1</h1>

 <p>
        这是一个在 <a href="https://morvanzhou.github.io/">莫烦Python</a>
<a href="https://morvanzhou.github.io/tutorials/scraping">爬虫教程</a> 中的简单测试.
    </p>

 ['https://morvanzhou.github.io/', 'https://morvanzhou.github.io/tutorials/scraping']

BeautifulSoup解析网页: CSS

# 先读取页面
from bs4 import BeautifulSoup
from urllib.request import urlopen

html=urlopen(
    "https://morvanzhou.github.io/static/scraping/list.html"
).read().decode('utf-8')
print(html)

# 将网页信息加载进BeautifulSoup,以lxml的形式
soup=BeautifulSoup(html,features='lxml')
<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>爬虫练习 列表 class | 莫烦 Python</title>
    <style>
    .jan {
        background-color: yellow;
    }
    .feb {
        font-size: 25px;
    }
    .month {
        color: red;
    }
    </style>
</head>

<body>

<h1>列表 爬虫练习</h1>

<p>这是一个在 <a href="https://morvanzhou.github.io/" >莫烦 Python</a> 的 <a href="https://morvanzhou.github.io/tutorials/scraping" >爬虫教程</a>
    里无敌简单的网页, 所有的 code 让你一目了然, 清晰无比.</p>

<ul>
    <li class="month">一月</li>
    <ul class="jan">
        <li>一月一号</li>
        <li>一月二号</li>
        <li>一月三号</li>
    </ul>
    <li class="feb month">二月</li>
    <li class="month">三月</li>
    <li class="month">四月</li>
    <li class="month">五月</li>
</ul>

</body>
</html>
# 按CSS class 匹配
month=soup.find_all('li',{
  "class":"month"})
for m in month:
    print(m.get_text())
一月
二月
三月
四月
五月
jan=soup.find('ul',{
  'class':'jan'})
d_jan=jan.find_all('li')
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值