提取HTML中所有URL链接
搜索到所有<a>标签
解析<a>标签格式,提取href后的链接内容!

import requests
r = requests.get("http://python123.io/ws/demo.html")
r.text
demo = r.text
from bs4 import BeautifulSoup
soup = BeautifulSoup(demo , "html.parser")
print(soup.prettify())
from bs4 import BeautifulSoup
Soup = BeautifulSoup(demo, “html.parser”)
for link in soup.find_all('a'):
print(link.get(‘href’))

python网络爬虫信息组织与提取_搜索

如果输入报错:

python网络爬虫信息组织与提取_字符串_02

python网络爬虫信息组织与提取_字符串_03

find_all(name)

python网络爬虫信息组织与提取_html_04

查找所有的tag name:
for tag in soup.find_all(True):
 Print(tag.name)

python网络爬虫信息组织与提取_搜索_05

引入正则表达式:import re

python网络爬虫信息组织与提取_字符串_06

 

python网络爬虫信息组织与提取_搜索_07

匹配含有‘b’的标签。并将其输出!
查找属性。必须带个‘p’。因为这个是一个类别。p中包含course字符串的信息!

 

python网络爬虫信息组织与提取_字符串_08

对属性做约束!看来通过,id=’link1’进行匹配的错误一个都不可以!

 

python网络爬虫信息组织与提取_字符串_09

模糊查找,就需要正则表达式啦!
Import re
soup.find_all(id=re.compile(‘link’))
以link开头,但是不完全一致!
用正则表达只需要给出一部分就可以进行模糊搜索!

python网络爬虫信息组织与提取_搜索_10

 soup.find_all('a',recursive=False)对子孙进行搜索。

python网络爬虫信息组织与提取_字符串_11

python网络爬虫信息组织与提取_html_12

String:<>...</>中字符串区域的检索字符串。

python网络爬虫信息组织与提取_html_13

 

python网络爬虫信息组织与提取_字符串_14

用过之后:

 

python网络爬虫信息组织与提取_搜索_15

<tag>(..)等价于<tag>.find_all(..)
soup(..)等价于 soup.find_all(..)

7个方法:

 

python网络爬虫信息组织与提取_html_16

python网络爬虫信息组织与提取_搜索_17

总结:

 

python网络爬虫信息组织与提取_字符串_18

python网络爬虫信息组织与提取_字符串_19

 

三种标记信息的比较:好多图系列~

YAML:

python网络爬虫信息组织与提取_搜索_20

JSON

 

python网络爬虫信息组织与提取_字符串_21

 

HTML:

python网络爬虫信息组织与提取_html_22

YAML1:

python网络爬虫信息组织与提取_字符串_23

python网络爬虫信息组织与提取_字符串_24

python网络爬虫信息组织与提取_html_25

python网络爬虫信息组织与提取_字符串_26

python网络爬虫信息组织与提取_html_27

python网络爬虫信息组织与提取_html_28

python网络爬虫信息组织与提取_搜索_29

 

python网络爬虫信息组织与提取_搜索_30

python网络爬虫信息组织与提取_字符串_31

python网络爬虫信息组织与提取_html_32