[問題] python爬蟲問題

看板Python作者 (誠實豆沙包)時間3年前 (2020/07/23 17:45), 編輯推噓-1(013)
留言4則, 2人參與, 3年前最新討論串1/1
小弟是爬蟲菜鳥新手 最近在學習如何爬蟲 從最基本的靜態網頁開始爬起 以下是我的CODE import requests from bs4 import BeautifulSoup import time url = "http://www.eslite.com/Search_BW.aspx?query=python&searchType=&page=1" html = requests.get(url).text soup = BeautifulSoup(html, 'html.parser') #先輸入的是要解析的文件名稱 後面是 parser page = 1 all_titles=[] def parse(html, page): print(page) all_td_tags = soup.find_all('td', class_="name") for item in all_td_tags: title=item.a.span.text.strip() all_titles.append(title) next_page_node = soup.find('a', id="ctl00_ContentPlaceHolder1_pager1_next") #下一頁的node print (next_page_node.get('href')) print("-------------------") if next_page_node.get('href') != "/Search_BW.aspx?query=python&searchType=&page=42": time.sleep(2) next_url = f"https://www.eslite.com{next_page_node.get('href')}" print(next_url) next_html = requests.get(next_url).text page +=1 parse(next_html, page) parse(html, page) for e in all_titles: print(e) 現在有的問題是我想要爬其他分頁的時候 next_page_node擷取到的href一直只會擷取到第二頁 然後就不動了 我想不太透是哪裡導致她永遠只會停在第二頁 麻煩各位30cm教教我~ 謝大大 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.65.162.112 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1595497548.A.7FC.html

07/23 18:37, 3年前 , 1F
你的request只產生了一次,parse一直執行同一個資料
07/23 18:37, 1F

07/23 19:24, 3年前 , 2F
next_html = requests.get(next_url).text
07/23 19:24, 2F

07/23 19:24, 3年前 , 3F
但這裡不是已經有再去request了嗎?
07/23 19:24, 3F

07/23 20:55, 3年前 , 4F
我知道發生什麼事了 感謝大大
07/23 20:55, 4F
文章代碼(AID): #1V6LnCVy (Python)