renjianxin 发表于 2022-3-20 16:10

本人萌新,大佬们帮忙看看代码哪里有错误,谢谢了

import requests
from bs4 import BeautifulSoup
url = "https://www.umeitu.com/bizhitupian/weimeibizhi/"
resp = requests.get(url)
resp.encoding = "utf-8"

main_page = BeautifulSoup(resp.text, "html.parser")
alist = main_page.find("div",class = "TypeList").find_all("a")
print(alist)
#for a in alist:

    print(a.get('href'))

提示alist = main_page.find("div",class = "TypeList").find_all("a")
                                 ^
SyntaxError: invalid syntax

xrryz 发表于 2022-3-20 17:58

本帖最后由 xrryz 于 2022-3-20 18:32 编辑

alist = main_page.find(name="div", attrs={"class" :"TypeList"})
...

main_page = BeautifulSoup(resp.text, "html.parser")
lis=main_page.select(".TypeList > ul:nth-child(1)")
for li in lis:
    try:
      u=li.select('a')['href']
      print(u)
    except:
      pass

SoftwareTesting 发表于 2022-3-20 18:00

试试class_ =

sgvvvv 发表于 2022-3-20 18:31

class要写成class_

swjia 发表于 2022-3-20 19:57

语法错误

52shijie 发表于 2022-3-20 20:45

class是python的关键字,使用 class 做参数会导致语法错误.从Beautiful Soup的4.1.1版本开始,可以通过 class_ 参数来搜索。

guogangan 发表于 2022-3-20 21:35

class语法错误,改一下吧

shige007 发表于 2022-3-20 22:15


import requests
from bs4 import BeautifulSoup
url = "https://www.umeitu.com/bizhitupian/weimeibizhi/"
resp = requests.get(url)
resp.encoding = "utf-8"

main_page = BeautifulSoup(resp.text, "html.parser")
alist = main_page.find("div",class_="TypeList").find_all("a")
print(alist)
for a in alist:
    print(a.get('href'))

renjianxin 发表于 2022-3-21 08:30

谢谢大家,class_就可以了,谢谢谢谢
页: [1]
查看完整版本: 本人萌新,大佬们帮忙看看代码哪里有错误,谢谢了