代码之家  ›  专栏  ›  技术社区  ›  Sid

用beautifulsoup抓取google时的syntaxerror

  •  0
  • Sid  · 技术社区  · 7 年前

    我正在抓取谷歌搜索结果。然而,我在做这件事的时候反复地得到一个语法错误。代码如下:

    import urllib.request
    from bs4 import BeautifulSoup
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/70.0'
    
    url = "https://www.google.com/search?hl=en&q=python+wikipedia"
    headers={'User-Agent':user_agent,} 
    
    request=urllib.request.Request(url,None,headers) #The assembled request
    response = urllib.request.urlopen(request)
    data = response.read()
    
    soup= BeautifulSoup(data, 'html.parser')
    l = soup.find_all('h' , 'attrs' = {"class":'LC20lb'})
    print(l)
    

    我得到:

    syntaxerror:关键字不能是表达式

    排队 l = soup.find_all('h' , 'attrs' = {"class":'LC20lb'}) 是的。有人能告诉我我做错了什么吗?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Petr Blahos    7 年前

    attrs周围不应该有撇号:

    l = soup.find_all('h' ,   attrs  = {"class":'LC20lb'})
    # not:                   _     _
    #l = soup.find_all('h' , 'attrs' = {"class":'LC20lb'})    
    #                        ^     ^
    
        2
  •  1
  •   krishna    7 年前
    import urllib.request
    from bs4 import BeautifulSoup
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/70.0'
    
    url = "https://www.google.com/search?hl=en&q=python+wikipedia"
    headers={'User-Agent':user_agent,}
    
    request=urllib.request.Request(url,None,headers) #The assembled request
    response = urllib.request.urlopen(request)
    data = response.read()
    
    soup= BeautifulSoup(data, 'html.parser')
    l = soup.find_all('h',  {"class":'LC20lb'})
    print(l)