这是我从一个网站上找到的html代码.

我在python中try 了很多方法来删除该链接,但它总是显示某种类型的错误,有时还会显示不同的链接.

<thead>
<tr class="footable-header">
<th scope="col" 
class="ninja_column_0 
ninja_clmn_nm_date ">Date</th><th scope="col"class="ninja_column_1ninja_clmn_nm_download">download</th></tr></thead><tbody><tr data-row_id="0" 
class="ninja_table_row_0 nt_row_id_0"><td>01-05-2022</td><td>https://vk.com/doc722551386_632783806? hash=gjIfCA0ILqZ1LQlzftCyxZ4zOATANYnUqZXiZ1vsAJH&dl=5wFKrFiIzvVfYJ6M4m1z9ALqKzGdXJdsGAXv1NaBtSg</td> </tr>

下面是我试过的python个代码:

import requests
from bs4 import BeautifulSoup

url="https://www.careerswave.in/dainik-jagran-newspaper-download/"
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text,'html.parser')
f = open("vkdain.txt", "w")
for link in soup.find_all("a"):
data = link.get('href')
print(data)

推荐答案

如果你只想获得td个链接,这对我来说很有用:

import requests
from bs4 import BeautifulSoup

url = "https://www.careerswave.in/dainik-jagran-newspaper-download/"
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'html.parser')
f = open("vkdain.txt", "w")
for link in soup.find_all("td"): # find all the td's
    if link.text.startswith('https://vk'): # check if the pattern is the one you want
        print(link.text)

这将得到以下结果:

https://vk.com/doc722551386_632783806?hash=gjIfCA0ILqZ1LQlzftCyxZ4zOATANYnUqZXiZ1vsAJH&dl=5wFKrFiIzvVfYJ6M4m1z9ALqKzGdXJdsGAXv1NaBtSg
https://vk.com/doc722551386_632705478?hash=mXInLmfkZNSLz5UVqRoRW60bRlzynUFUpRZoiBeW4ko&dl=zFzHm0Edhycg4ulJp33jdeFbypSaynNcjpZ41cUnID0
...
https://vk.com/doc623586997_607921843?hash=c6f706ee5f09f4d4e5&dl=f780520e509b9f671b
https://vk.com/doc623586997_607809766?hash=ef486a0fb1e873640e&dl=eeb60781cef9e58541

以下是一些相关问题:

Python相关问答推荐

两极:滚动组,起始指数由不同列设置

Python中的锁定类和线程以实现dict移动

如何使用Tkinter创建两个高度相同的框架(顶部和底部)?

从今天起的future 12个月内使用Python迭代

Python -Polars库中的滚动索引?

有什么方法可以避免使用许多if陈述

在Python中为变量的缺失值创建虚拟值

使用pandas、matplotlib和Yearbox绘制时显示错误的年份

韦尔福德方差与Numpy方差不同

SQLGory-file包FilField不允许提供自定义文件名,自动将文件保存为未命名

如何检测背景有噪的图像中的正方形

查找两极rame中组之间的所有差异

将输入管道传输到正在运行的Python脚本中

为什么默认情况下所有Python类都是可调用的?

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

启用/禁用shiny 的自动重新加载

Polars asof在下一个可用日期加入

如何在Pyplot表中舍入值

AES—256—CBC加密在Python和PHP中返回不同的结果,HELPPP

ConversationalRetrivalChain引发键错误