0
点赞
收藏
分享

微信扫一扫

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

辰鑫chenxin 2022-07-12 阅读 67


提取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网络爬虫信息组织与提取_html

如果输入报错:

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

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

find_all(name)

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

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

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

引入正则表达式:import re

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

 

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

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

 

python网络爬虫信息组织与提取_html_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网络爬虫信息组织与提取_字符串_12

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

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

 

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

用过之后:

 

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

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

7个方法:

 

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

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

总结:

 

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

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

 

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

YAML:

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

JSON

 

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

 

HTML:

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

YAML1:

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

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

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

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

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

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

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

 

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

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

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

 

 

 

举报

相关推荐

0 条评论