Python3.5使用google-search模块运行代码时出现报错求助
Hey, let's figure out why your Google search code is throwing an error in Python 3.5! The issue you're hitting is almost certainly tied to the outdated google-search module you're using—it's no longer maintained, has compatibility issues with Python 3.5, and likely has a broken import structure. Here's how to fix it:
Option 1: Switch to a maintained alternative (Recommended)
The googlesearch-python package is the actively updated replacement for the old google-search module. Let's swap it in:
- First, remove the outdated package:
pip uninstall google-search - Install the maintained version:
pip install googlesearch-python - Update your code to match the new API (it's simpler and more reliable):
If you need to extract titles and content (like your original code tried to do), combine the search withfrom googlesearch import search # Search for your query, adjust num_results as needed for result_url in search("anything", num_results=5): print("Result URL:", result_url)requestsandBeautifulSoupto parse the pages:from googlesearch import search import requests from bs4 import BeautifulSoup for url in search("anything", num_results=3): try: # Fetch the page content page_response = requests.get(url, timeout=5) soup = BeautifulSoup(page_response.text, 'html.parser') # Extract title and a snippet of content page_title = soup.title.string if soup.title else "No title found" page_content = ' '.join([p.get_text() for p in soup.find_all('p')[:3]]) print("Title:", page_title) print("Content snippet:", page_content) print("---") except Exception as e: print(f"Couldn't load {url}: {str(e)}")
Option 2: Fix the old module (Not recommended)
If you really need to stick with the old google-search package, try these steps:
- Reinstall the package to fix any corrupted files, using a version that works with Python 3.5:
pip uninstall google-search pip install google-search==1.0.2 - Correct the import statement—some old versions use a simpler path:
from googlesearch import GoogleSearch response = GoogleSearch().search("anything") for result in response.results: print("Title:", result.title) print("Content:", result.getText())
One last note: Python 3.5 is no longer supported by the Python Software Foundation. Upgrading to Python 3.7 or later will help you avoid compatibility issues like this in the future.
内容的提问来源于stack exchange,提问作者Alfred George




