【问题标题】:BeautifulSoup not returning linksBeautifulSoup 不返回链接
【发布时间】:2022-11-19 04:39:17
【问题描述】:

对于我的 python 训练营,我正在尝试从该站点创建文章日志,并返回最高赞成票。其余代码有效,但我无法让它正确返回 href。我得到“没有”。我已经尝试了我所知道的一切......任何人都可以提供任何指导吗?

from bs4 import BeautifulSoup
import requests


response = requests.get("https://news.ycombinator.com/")
yc_web_page = response.text


soup = BeautifulSoup(yc_web_page, "html.parser")
articles = soup.find_all(name="span", class_="titleline")

article_texts = []
article_links = []

for article_tag in articles:

    article_text = article_tag.get_text()
    article_texts.append(article_text)

    article_link = article_tag.get("href")
    article_links.append(article_link)



article_upvotes = [int(score.getText().split()[0]) for score in soup.find_all(name="span", class_="score")]


largest_number = max(article_upvotes)
largest_index = article_upvotes.index(largest_number)

print(article_texts[largest_index])
print(article_links[largest_index])
print(article_upvotes[largest_index])`

我试图将“href”更改为“a”标签,但它返回了相同的值“none”

【问题讨论】:

    标签: python parsing beautifulsoup html-parsing


    【解决方案1】:

    尝试:

    
    ...
    
        article_link = article_tag.a.get("href")    # <--- put .a here
    
    ...
    

    from bs4 import BeautifulSoup
    import requests
    
    
    response = requests.get("https://news.ycombinator.com/")
    yc_web_page = response.text
    
    
    soup = BeautifulSoup(yc_web_page, "html.parser")
    articles = soup.find_all(name="span", class_="titleline")
    
    article_texts = []
    article_links = []
    
    for article_tag in articles:
    
        article_text = article_tag.get_text()
        article_texts.append(article_text)
    
        article_link = article_tag.a.get("href")   # <--- put .a here
        article_links.append(article_link)
    
    
    article_upvotes = [
        int(score.getText().split()[0])
        for score in soup.find_all(name="span", class_="score")
    ]
    
    
    largest_number = max(article_upvotes)
    largest_index = article_upvotes.index(largest_number)
    
    print(article_texts[largest_index])
    print(article_links[largest_index])
    print(article_upvotes[largest_index])
    

    印刷:

    Fred Brooks has died (twitter.com/stevebellovin)
    https://twitter.com/stevebellovin/status/1593414068634734592
    1368
    

    【讨论】:

    • 谢谢!那行得通
    【解决方案2】:

    这是一点更短方法:

    import requests
    from bs4 import BeautifulSoup
    
    url = "https://news.ycombinator.com/"
    
    soup = BeautifulSoup(requests.get(url).text, "lxml")
    
    all_scores = [
        [
            int(x.getText().replace(" points", "")),
            x["id"].replace("score_", ""),
        ]
        for x in soup.find_all("span", class_="score")
    ]
    
    votes, tr_id = sorted(all_scores, key=lambda x: x[0], reverse=True)[0]
    
    table_row = soup.find("tr", id=tr_id)
    text = table_row.select_one("span a").getText()
    link = table_row.select_one("span a")["href"]
    
    print(f"{text}
    {link}
    {votes} votes")
    

    输出:

    Fred Brooks has died
    https://twitter.com/stevebellovin/status/1593414068634734592
    1377 votes
    

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2013-05-30
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多