在Python中,可以使用多種方式遍歷網(wǎng)頁(yè),以下是兩種常見的方法:
import requests
from bs4 import BeautifulSoup
# 發(fā)送HTTP請(qǐng)求獲取網(wǎng)頁(yè)內(nèi)容
response = requests.get('http://example.com')
html_content = response.text
# 解析HTML內(nèi)容
soup = BeautifulSoup(html_content, 'html.parser')
# 遍歷網(wǎng)頁(yè)上的所有鏈接
for link in soup.find_all('a'):
print(link.get('href'))
import scrapy
class MySpider(scrapy.Spider):
name = 'example'
start_urls = ['http://example.com']
def parse(self, response):
# 遍歷網(wǎng)頁(yè)上的所有鏈接
for link in response.css('a::attr(href)').getall():
yield {
'link': link
}
以上是兩種常見的方法,根據(jù)具體的需求選擇合適的方式進(jìn)行網(wǎng)頁(yè)遍歷。