python - 检查字符串是否具有 .pdf 扩展名
问题描述
我对刮擦很陌生。我有2个问题。第一个是我需要废弃网站中包含锚标签的特定部分。我只需要获取锚标签 pdf 链接及其标题,但不幸的是,锚标签也有正常的链接。这是我的第一个问题
第二个问题是输出有不需要的线路中断。对于这两个问题,代码是相同的。对于相同的代码,我有这两个问题。
网站.html
<div>
<a href="www.url.com/somethin.pdf">pdf
link</a>
<a href="www.url.com/somethin.pdf">pdf
link</a>
<a href="www.url.com/somethin">normal
link</a>
</div>
剪贴画.py
import requests
from bs4 import BeautifulSoup
page = requests.get('https://www.privacy.gov.ph/advisories/')
soup = BeautifulSoup(page.content,'html.parser')
section = soup.find("section", {"class": "news_content"})
for link in section.find_all("a"):
pdf = link['href'].replace("..", "")
title = link.text.strip()
print("title: " + title + "\t")
print("pdf_link: " + pdf + "\t")
print('\n')
如果您运行此代码,您会发现该 html 代码的标题具有不需要的换行符
解决方案
您可以使用正则表达式来获取以 pdf 扩展名结尾的 href。对于不需要的换行符,我不确定您的意思。我只能假设您的意思是每次打印之间有 2 条新行。如果这个假设是正确的,那是因为每个print
函数都将在一个新行上。因此,当您在那里时print('\n')
,它将在新行上打印,然后再打印新行。如果您只想要 1 个空格,请删除最后一个打印功能并将其更改\t
为\n
import requests
from bs4 import BeautifulSoup
import re
page = requests.get('https://www.privacy.gov.ph/advisories/')
soup = BeautifulSoup(page.content,'html.parser')
section = soup.find("section", {"class": "news_content"})
links = section.findAll(href=re.compile("\.pdf$")) # <---- SEE HERE
for link in links:
pdf = link['href'].replace("..", "")
title = link.text.strip().replace('\n','')
print("title: " + title)
print("pdf_link: " + pdf + "\n")
输出:
title: Updated Templates on Security Incident and Personal Data Breach Reportorial Requirements
pdf_link: https://www.privacy.gov.ph/wp-content/files/attachments/nwsltr/Final_Advisory18-02_6.26.18.pdf
title: Guidelines on Privacy Impact Assessments
pdf_link: https://www.privacy.gov.ph/wp-content/files/attachments/nwsltr/NPC_AdvisoryNo.2017-03.pdf
title: Access to Personal Data Sheets of Government Personnel
pdf_link: https://www.privacy.gov.ph/wp-content/files/attachments/nwsltr/NPC_Advisory_No.2017-02.pdf
推荐阅读
- php - 错误:未找到“League\Flysystem\AwsS3v3\AwsS3Adapter”类
- c - 使用长变量存储字符并打印
- linux - bash while 循环中的“完成”功能/含义是什么?
- azure - Microsoft Azure 基础 Pdf 版
- apache-spark - 将 Dataset 中的嵌套 json 字符串转换为 Spark Scala 中的 Dataset/Dataframe
- ios - 将 IAP 存储在 UserDefaults 中,这样用户就不需要在每次按下“可购买”按钮时恢复购买
- c++ - 析构函数中的 delete[]:指定给 RtlValidateHeap 的地址无效
- react-native - 如何压缩刚用 React Native Expo 拍摄的图片
- flutter - HTTP 请求结果不可见
- javascript - React Native useEffect 在异步函数中的使用