它只打印第一页,而不会打印另一页 问题出在哪里? 我是Python的新手
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import requests
for num in range(1,6): #Number of pages plus one
url = (f'https://test.com/%D9%84-%8%B6/{num}')
html = urlopen(url)
r = requests.get(url)
bs = BeautifulSoup(html,'html.parser')
答案 0 :(得分:0)
您想做这样的事情:
from bs4 import BeautifulSoup
import requests
for num in range(1, 6): # Number of pages plus one
url = f"https://google.com/{num}"
r = requests.get(url)
bs = BeautifulSoup(r.text, "html.parser")
print(bs)
不需要 urllib.request import urlopen
。
关于下面的评论,这里是用BeautifulSoup
获得我的website的标题:
from bs4 import BeautifulSoup
import requests
url = "https://self.st" # You would have your number here.
r = requests.get(url)
bs = BeautifulSoup(r.text, "html.parser")
title = bs.find("div", {"class": "title"})
print(title)
print(title.text)
输出:
<div class="title">felipe faria</div>
felipe faria