简体   繁体   English

如何从 HTML 代码中的特定名称获取“href”值

[英]How to get the 'href' value from a specific name in the HTML code

I want to create a code which gets the href=... of a specific name in the HTML code.我想创建一个代码来获取 HTML 代码中特定名称的 href=... 。 A part of the HTML code looks like this:部分 HTML 代码如下所示:

class="name-link" href="/shop/t-shirts/hcir4dp5z/ntsf95gld">Tupac Hologram Tee</a> == $0

There are more parts in the HTML code that look like this. HTML 代码中有更多部分看起来像这样。 Because there are multiple products on the website (Supreme).因为网站上有多个产品(至尊)。 I have got the name of the product -> Tupac Hologram Tee .我有产品名称 -> Tupac Hologram Tee 。 Does someone know how I can get the href value from this specific line instead of a other line in the html code which also includes a href= ?有人知道我如何从这个特定行而不是 html 代码中的其他行获取 href 值,其中还包括一个 href= 吗?

This was my code which printed out all the href links on the page.这是我的代码,它打印出页面上的所有 href 链接。 But I just want that specific link from the product with Tupac Hologram Tee as the name.但我只想要来自以 Tupac Hologram T 恤为名称的产品的特定链接。

session = HTMLSession()
r = session.get(url)
word = r.html.links
print(word)

If your code prints a list of all lines including href, couldn't you just search for the line you want from them?如果您的代码打印了包括 href 在内的所有行的列表,您不能只从它们中搜索您想要的行吗? It would be a bit easier to help if the output and more code was visible.如果输出和更多代码可见,帮助会更容易一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM