python爬虫(一)爬取基本网页内容

361次阅读
没有评论

参考莫烦python

#导入打开链接的库和正则 from urllib.request import urlopen import re

#选择所有内容 # if has Chinese, apply decode() html = urlopen("https://morvanzhou.github.io/static/scraping/basic-structure.html").read().decode('utf-8') print(html)

找标题内容 res = re.findall(r"<title>(.+?)</title>", html) print("\nPage title is: ", res[0]) #找段落内容 res1 = re.findall(r"<p>(.+?)</p>", html,flags=re.DOTALL) #flag这句是多行找 print("\nPage paragraphy is: ", res1[0]) #找链接 res2=re.findall(r'href="(.*?)"',html) print("\nAll links: ", res2)

正则就是选文本的方法

结果如右图

python爬虫(一)爬取基本网页内容

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-27发表,共计496字。
新手QQ群:570568346,欢迎进群讨论 Python51学习