我正在嘗試刮取一個帶有分頁功能的網頁。
代碼:
from requests_html import _URL, HTMLSession
from bs4 import BeautifulSoup
for page in range(1,6)。
s = HTMLSession()
url = 'https://www.lazada.com.ph/catalog/?q=laptop': s = HTMLSession()
url = '&page={}'
r=s.get(url.format(page))
print(url)
輸出:
https://www.lazada.com.ph/catalog/?q=laptop&page={}。
https://www.lazada.com.ph/catalog/?q=laptop&page={}
https://www.lazada.com.ph/catalog/?q=laptop&page={}
https://www.lazada.com.ph/catalog/?q=laptop&page={}
https://www.lazada.com.ph/catalog/?q=laptop&page={}
期望:
https://www.lazada.com.ph/catalog/?q=laptop&page={1}。
https://www.lazada.com.ph/catalog/?q=laptop&page={2}。
https://www.lazada.com.ph/catalog/?q=laptop&page={3}。
https://www.lazada.com.ph/catalog/?q=laptop&page={4}。
https://www.lazada.com.ph/catalog/?q=laptop&page={5}。
我還是個新手,正在學習python,請幫助我獲得我的預期結果。預先感謝。
uj5u.com熱心網友回復:
如果你的python版本支持f字串...
from requests_html import _URL,HTMLSession
from bs4 import BeautifulSoup
for page in range(1,6)。
s = HTMLSession()
url = f'https://www.lazada.com.ph/catalog/?q=laptop&page={page}'。
r = s.get(url)
print(url)
uj5u.com熱心網友回復:
url沒有被修改,試試:
from requests_html import _URL,HTMLSession
from bs4 import BeautifulSoup
for page in range(1,6)。
s = HTMLSession()
url = 'https://www.lazada.com.ph/catalog/?q=laptop': s = HTMLSession()
url = '&page={}'/span>
url = url.format(page)
r = s.get(url)
print(url)
或者更好:
from requests_html import _URL, HTMLSession
from bs4 import BeautifulSoup
for page in range(1,6)。
s = HTMLSession()
url = 'https://www.lazada.com.ph/catalog/?q=laptop&page={}'.format(page)
r = s.get(url)
print(url)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312338.html
標籤:
上一篇:5大開發語言同時計算素數,誰快?
