作者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://webptt.com/cn.aspx?n=bbs/Python/M.1595497548.A.7FC.html
1F:嘘 TakiDog: 你的request只产生了一次,parse一直执行同一个资料 07/23 18:37
2F:→ shot0512: next_html = requests.get(next_url).text 07/23 19:24
3F:→ shot0512: 但这里不是已经有再去request了吗? 07/23 19:24
4F:→ shot0512: 我知道发生什麽事了 感谢大大 07/23 20:55
5F:推 a28503662: 应该要再给soup解析一次吧~ 08/12 13:05