1. 程式人生 > >python find()函式

python find()函式

函式原型:find(str, pos_start, pos_end)

解釋:

  • str:被查詢“字串”
  • pos_start:查詢的首字母位置(從0開始計數。預設:0
  • pos_end: 查詢的末尾位置(預設-1

返回值:如果查到:返回查詢的第一個出現的位置。否則,返回-1

    request = urllib.request.Request(url=url, headers=headers)
    feeddata = urllib.request.urlopen(request).read().decode('utf-8')
    soup = BeautifulSoup(feeddata, 'html.parser')
    content = soup.find_all("div", class_="lvpic pic img left", iid=True, limit=4)
    result = []
    for i in content:
        s = str(i)
        l = s.find("iid=")
        if l == -1:
            continue
        r = s.find("\">")
        result.append(str(s[l + 5:r]))
    print(result)