0
点赞
收藏
分享

微信扫一扫

beautifulsoup以及正则表达式re之间的一些知识!

_karen 2022-07-12 阅读 35


代码:

import requests
import re
from bs4 import BeautifulSoup
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
print(demo)
print('查找:ID属性为固定值得东西!')
print(soup.find_all(href="http://www.icourse163.org/course/BIT-268001"))
print('\n')
print(soup.find_all(id = re.compile('n')))#仅仅查找id = 包含引号里面内容的东西!
print('查找a标签:')
print(soup.find_all('a'))
print('new try!:')
print(soup.find_all(string = 'Advanced Python'))
print('a new try!:')
print(soup.find_all(string = re.compile('python')))
# print('findAll(tag, attributes, recursive, text, limit, keywords)这个是使用方法!')
# print('输出course属性值的p标签:')
# print(soup.find_all('p', 'course'))
# print('返回包含d的所有标签的名称!')
# for tag in soup.find_all(re.compile('d')):
# print(tag.name)

结果:

 

D:\python_install\python.exe D:/pycharmworkspace/temp1/crawler_1.py
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>
</body></html>
查找:ID属性为固定值得东西!
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>]


[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
查找a标签:
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
new try!:
['Advanced Python']
a new try!:
[]

Process finished with exit code 0

 

举报

相关推荐

0 条评论