2017-01-22 110 views
-2

我有一個鏈接列表,每個鏈接都包含多個頁面。我發現每個子類別中的頁面數量,但現在我想進行循環以遍歷子鏈接的所有頁面。因此,鏈接的第一類將有8頁,第二個鏈接將有6頁等等。將列表引用到for循環中

lists = [8, 6, 5, 13, 10, 16, 13, 15, 4, 4, 5, 7, 2, 6, 6, 8, 9, 8, 3, 8, 8, 1, 6, 3, 2, 15, 5, 4, 2, 12, 18, 5, 2] 

import bs4 as bs 
import urllib.request 
import pandas as pd 
import urllib.parse 
import re 


#source = urllib.request.urlopen('https://messageboards.webmd.com/').read() 
source = urllib.request.urlopen('https://messageboards.webmd.com').read() 
soup = bs.BeautifulSoup(source,'lxml') 


df = pd.DataFrame(columns = ['link'],data=[url.a.get('href') for url in soup.find_all('div',class_="link")]) 
lists =[] 
lists2=[] 
lists3=[] 
page_links = [] 


for i in range(0,33): 
    link = (df.link.iloc[i]) 
    req = urllib.request.Request(link) 
    resp = urllib.request.urlopen(req) 
    respData = resp.read() 
    temp1=re.findall(r'Filter by</span>(.*?)data-pagedcontenturl',str(respData)) 
    temp1=re.findall(r'data-totalitems=(.*?)data-pagekey',str(temp1))[0] 
    pageunm=round(int(re.sub("[^0-9]","",temp1))/10) 
    lists.append(pageunm) 


for j in lists: 
    for y in range(1, j+1): 
     url_pages = link + '#pi157388622=' + str(j) 
     page_links.append(url_pages) 
+0

所以每個'j'要遍歷'範圍內(1,J + 1)'? – jonrsharpe

回答

1

使用嵌套循環:

for i in lists: # [8, 6, 5, etc] 
    # now use i for the inner loop 
    for j in range(1, i+1): # [1-8], [1-6], [1-5], etc 
     url_pages = link + '#pi157388622=' + str(j) 
     # do sth with url_pages, or it'll be just overwritten each iteration 
+0

你能否進一步解釋你最後的評論是什麼意思?你的意思是將它們輸入另一個列表中嗎? – Data1234

+0

@ Data1234好吧,語句'url_pages = foo'只給本地變量'url_pages'賦值,但別的什麼都不做。在下一次循環迭代中,這個值將會丟失,因爲'url_pages'將被重新分配一個新值。因此,你應該做某事。與它一樣,打印或將其添加到列表中... – schwobaseggl

+0

它不起作用。雖然我沒有解釋我的自我權利。 – Data1234