1. 程式人生 > >Python SyntaxError: unexpected EOF while parsing

Python SyntaxError: unexpected EOF while parsing

程式碼:

import url_get
from bs4 import BeautifulSoup
import re


def news_select(url,fold_name,*selectors):
    
    html = url_get.get_html(url) #獲取html
    soup = BeautifulSoup(html,'html.parser')  #定義一個Soup物件

    #newses = soup.select('ul.pic_lists > li')

    for selector in selectors:
        try:
            newes = soup.select(selector) # 一定注意selector的正確性
            href = soup.find('a',href=re.compile(r"[0-9a-zA-Z/.]{1,}")) #匹配href
            print(href['href'])
            print(item.get_text().strip()) 	#去掉首位空格

報錯

SyntaxError: unexpected EOF while parsing

原因

寫了try,忘寫except…