2016-02-26 219 views
0

我在網站上獲取公司和產品詳細信息。 它有div標籤,裏面有li標籤,我想要獲得div標籤中的所有li標籤。 我使用python 3.5.1和BeautifulSoup如何獲取div標籤中的所有li標籤

我的代碼:

from bs4 import BeautifulSoup 
import urllib.request 
import re 
r = urllib.request.urlopen('http://i.cantonfair.org.cn/en/ExpExhibitorList.aspx?k=glassware') 
soup = BeautifulSoup(r, "html.parser") 

links = soup.find_all("a", href=re.compile(r"expexhibitorlist\.aspx\?categoryno=[0-9]+")) 
linksfromcategories = ([link["href"] for link in links]) 

string = "http://i.cantonfair.org.cn/en/" 
linksfromcategories = [string + x for x in linksfromcategories] 

for link in linksfromcategories: 
    response = urllib.request.urlopen(link) 
    soup2 = BeautifulSoup(response, "html.parser") 
    links2 = soup2.find_all("a", href=re.compile(r"\ExpExhibitorList\.aspx\?categoryno=[0-9]+")) 
    linksfromsubcategories = ([link["href"] for link in links2]) 
    linksfromsubcategories = [string + x for x in linksfromsubcategories] 
    for link in linksfromsubcategories: 
     response = urllib.request.urlopen(link) 
     soup3 = BeautifulSoup(response, "html.parser") 
     links3 = soup3.find_all("a", href=re.compile(r"\ExpExhibitorList\.aspx\?categoryno=[0-9]+")) 
     linksfromsubcategories2 = ([link["href"] for link in links3]) 
     linksfromsubcategories2 = [string + x for x in linksfromsubcategories2] 
     for link in linksfromsubcategories2: 
      response2 = urllib.request.urlopen(link) 
      soup4 = BeautifulSoup(response2, "html.parser") 
      companylink = soup4.find_all("a", href=re.compile(r"\expCompany\.aspx\?corpid=[0-9]+")) 
      companylink = ([link["href"] for link in companylink]) 
      companylink = [string + x for x in companylink] 
      for link in companylink: 
       response3 = urllib.request.urlopen(link) 
       soup5 = BeautifulSoup(response3, "html.parser") 
       companydetail = soup5.find_all("div", id="contact") 
       for element in companydetail: 
        companyname = element.a[0].get_text() 
        print (companyname) 
        companyaddress = element.a[1].get_text() 
        print (companyaddress)And I am getting error 

而且我得到錯誤

Traceback (most recent call last): 
    File "D:\python\phase3.py", line 54, in <module> 
    lis = companydetail.find_all('li') 
AttributeError: 'ResultSet' object has no attribute 'find_all' 
+0

它說的:

for d in companydetail: lis = d.find_all('li') 

或者使用列表中理解的companydetail得到所有li s的列表第54行有錯誤,但是您只包含了37行,其中沒有一行包含拋出錯誤的代碼。 – wpercy

回答

1

companydetailResultSet。也就是說,它是一個包含許多元素的可迭代對象(如listset)。由於您嘗試在此ResultSet對象上嘗試調用.find_all(),因此發生此錯誤。你應該通過這個對象像下面這樣迭代,並呼籲元素find_all()ResultSet

lis = [ li for d.find_all('li') for d in companydetail ] 
+0

好的這個作品但我得到兩次列表。爲什麼? –

+0

你是什麼意思「獲得兩次列表」? – wpercy

+0

像從李atg我得到公司的詳細信息,如姓名和電子郵件ID,但該名稱和電子郵件ID是兩次。 可能是我颳了兩次URL? –