简体   繁体   English

python 从 url 或元素按钮抓取页面 + 下一页循环

[英]python scrape page + next page loop from url or element button

It just prints the first page does not go and prints the other page where is the problem?它只打印第一页而不是 go 并打印另一页 问题出在哪里? I'm new to Python我是 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')

You want to be doing something like so:你想做这样的事情:

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 is not needed for this.为此不需要urllib.request import urlopen


Regarding the comments below, here is getting the title of my website with BeautifulSoup :关于下面的评论,这里是用BeautifulSoup获得我的网站的标题:

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)

Outputs:输出:

<div class="title">felipe faria</div>
felipe faria

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM