Error finding a string in another string in Python
In my perspective, the error you're encountering is due to the fact that some of the href values returned by link.get('href') may be None, and you cannot iterate over a None value. To avoid this error, you can add a condition to check if href is not None before checking if the word is in it. Here's the modified code:
from bs4 import BeautifulSoup
import requests

def list_image_links(url):
    response = requests.get(url)

    soup = BeautifulSoup(response.content, "html.parser")
    image_links = []
    for image_links in soup.find_all('a'):
        href = image_links.get('href')
        word = 'attach'
        
        if href and word in href:
            image_links.append(href)
    
# rest of the code...

The only change I made was adding an additional condition to the if clause, specifically if href and word in href:. I think this modification will resolve the issues.

I'm trying to return links that have the word "attach" in a list using Python on a web page. But every method I use to find that word in the link gives almost the same error.
This is my code and the error is received from line 16:
from bs4 import BeautifulSoup

# pip install requests
import requests

def list_image_links(url):
    # Send a GET request to the URL
    response = requests.get(url)

    soup = BeautifulSoup(response.content, "html.parser")
    image_links = []
    for image_links in soup.find_all('a'):
        href = image_links.get('href')
#if attach is in href
        word = 'attach'
        if word in href:
            image_links.append(href)
    
    print(image_links)
    return image_links

list_image_links('https://forum.ubuntu.ir/index.php?topic=211.0')


Error:
Traceback (most recent call last):
  File "d:\...\main.py", line 22, in <module>
    list_image_links('https://forum.ubuntu.ir/index.php?topic=211.0')
  File "d:\...\main.py", line 16, in list_image_links
    if word in href:
       ^^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable

