【问题标题】:Python 3 BeautifulSoup get URL (href or baseURL) if div Class caption text contains "English"Python 3 BeautifulSoup 获取 URL(href 或 baseURL)如果 div 类标题文本包含“英语”
【发布时间】:2022-12-10 09:12:39
【问题描述】:
<div class="gallery" data-tags="19 16 40193 41706 40476 7921 815 425 900 362 229 154 146 13 65 129 766 25 9 51931 188">
    <a href="/g/987654/" class="cover" style="padding:0 0 142.79999999999998% 0">
    <img is="lazyload-image" class="" width="250" height="357" data-src="https://abc.cloud.xyz/galleries/123456/thumb.jpg" alt="" src="https://abc.cloud.xyz/galleries/123456/thumb.jpg">
    <div class="caption">[User] Text ABCDEFGH [English] </div>
    </a>
</div>

该程序不会将 URL/href 保存到 txt 文件中。我认为它找不到 href

如果带有类标题的 div 元素包含 Word English,则应将元素类封面的 href (/g/987654/) 保存在 txt 文件中。

from bs4 import BeautifulSoup
import requests

url = "https://google.com"

response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

base_urls = []
for div in soup.find_all("div", {"class": "caption"}):
    if "English" in div.text:
        a_tag = div.find_previous_sibling("a")
        if a_tag:
            base_urls.append(a_tag["baseURL"])

with open("base_urls.txt", "w") as f:
    for base_url in base_urls:
        f.write(base_url + "\n")

**到目前为止我试过什么 ** 此代码有效,但它将所有 href 保存到 txt 文件中......

from bs4 import BeautifulSoup
import requests

url = "https://google.com"

page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")

links = soup.find_all("a")

hrefs = [link["href"] for link in links]

with open("links_test1.txt", "w") as file:
    for href in hrefs:
        file.write(href + "\n")
    from bs4 import BeautifulSoup
    import requests
    
   
    lurl = ["https://web.com/page1","https://web.com/page2","https://web.com/page3"]
    
    
    for url in lurl:
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
 
    base_urls = []
for div in soup.find_all("div", {"class": "caption"}):
    if "English" in div.text:
        a_tag = div.find_previous("a")
        if a_tag:
            base_urls.append(a_tag["href"])
with open("base_urls2.txt", "w") as f:
    for base_url in base_urls:
        f.write(base_url + "\n")

【问题讨论】:

    标签: python html beautifulsoup


    【解决方案1】:

    查看 HTML sn-p,您应该使用 .find_previous 而不是 .find_previous_sibling。另外,使用a_tag['href'],而不是a_tag['baseURL']

    from bs4 import BeautifulSoup
    
    
    html_doc = """
    <div class="gallery" data-tags="19 16 40193 41706 40476 7921 815 425 900 362 229 154 146 13 65 129 766 25 9 51931 188">
        <a href="/g/987654/" class="cover" style="padding:0 0 142.79999999999998% 0">
        <img is="lazyload-image" class="" width="250" height="357" data-src="https://abc.cloud.xyz/galleries/123456/thumb.jpg" alt="" src="https://abc.cloud.xyz/galleries/123456/thumb.jpg">
        <div class="caption">[User] Text ABCDEFGH [English] </div>
        </a>
    </div>"""
    
    soup = BeautifulSoup(html_doc, "html.parser")
    
    
    base_urls = []
    for div in soup.find_all("div", {"class": "caption"}):
        if "English" in div.text:
            a_tag = div.find_previous("a")
            if a_tag:
                base_urls.append(a_tag["href"])
    
    print(base_urls)
    

    印刷:

    ['/g/987654/']
    

    【讨论】:

    • 谢谢,效果很好 ;-) 还有一个问题...现在我想给变量 url 一个列表而不是单个 URL。我绑了它但是出现了这个错误,我该如何解决? ''' InvalidSchema No connection adapters were found for "['web1.com/page1', 'web1.com/page2', 'web1.com/page3']' line 9, in <module> response = requests.get(url) ''' 我应该做一个新的线程/问题帖子???
    • @Kirizu 你错过了https://。试试base_urls.append('https://web1.com' + a_tag["href"])
    • https is there but is not shown in the comment
    • @Kirizu url 是一个列表吗?然后尝试遍历此列表的元素并分别在每个元素上使用requests.get
    • 我试过这个(参见问题按钮)但它不起作用....没有错误但 txt 中也没有 URL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    相关资源
    最近更新 更多