作者shot0512 (誠實豆沙包)
看板Python
標題[問題] python爬蟲問題
時間Thu Jul 23 17:45:44 2020
小弟是爬蟲菜鳥新手
最近在學習如何爬蟲
從最基本的靜態網頁開始爬起
以下是我的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
噓 TakiDog: 你的request只產生了一次,parse一直執行同一個資料 07/23 18:37
→ shot0512: next_html = requests.get(next_url).text 07/23 19:24
→ shot0512: 但這裡不是已經有再去request了嗎? 07/23 19:24
→ shot0512: 我知道發生什麼事了 感謝大大 07/23 20:55
推 a28503662: 應該要再給soup解析一次吧~ 08/12 13:05