【问题标题】:BeautifulSoup's find method returning None instead of a linkBeautifulSoup 的 find 方法返回 None 而不是链接
【发布时间】:2020-10-05 10:19:13
【问题描述】:

感谢您在这里查看我的问题,我正在尝试从旧的 Reddit 博客页面获取下一页链接 但不知何故 find 方法返回我 None 对象,代码:

 def crawl(self):
        curr_page_url = self.start_url
        curr_page = requests.get(curr_page_url)
        bs = BeautifulSoup(curr_page.text,'lxml')
        # all_links = GetAllLinks(self.start_url)
        nxtlink = bs.find('a',attrs={'rel':'nofollow next'})['href']
        print(nxtlink)

并且 HTML 页面链接是此页面上的 Old Reddit page link 我正在尝试获取下一页的链接 在一个跨度标签中:

<span class="next-button">
    <a href="https://old.reddit.com/r/learnprogramming/?count=25&amp;after=t3_j54ae2" rel="nofollow 
    next">next ›
    </a>
</span>

【问题讨论】:

    标签: python web web-scraping beautifulsoup


    【解决方案1】:

    我认为你必须在请求中添加标头,否则服务器认为你是机器人,这是对的。

    试试这个:

    import requests
    from bs4 import BeautifulSoup
    
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-GB,en;q=0.5",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
    }
    
    response = requests.get("https://old.reddit.com/r/learnprogramming/", headers=headers).text
    soup = BeautifulSoup(response, "html.parser").find('a', attrs={'rel': 'nofollow next'})['href']
    print(soup)
    
    

    输出:

    https://old.reddit.com/r/learnprogramming/?count=25&after=t3_j5ezm8
    

    【讨论】:

    • 非常感谢,我沮丧了一个小时后几乎放弃了。它就像魔术一样工作:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多