python - Python 与嵌套 for 循环的混淆
问题描述
from requests_html import HTMLSession
def get_url(search_text):
session = HTMLSession()
template = 'https://www.amazon.com/s?k={}'
search_term = search_text.replace(' ', '+')
url = template.format(search_term)
url += '&pages={}'
products = []
下面是我感到困惑的部分
for x in range(1,21):
url_inc = url.format(x)
r = session.get(url_inc)
r.html.render(sleep=1)
for item in r.html.xpath('//*[@class="a-size-medium a-color-base a-text-normal"]'):
product = item.text
products.append(product)
return products
这是将在 main() 上运行的函数之一。
我希望该函数在内部更新 x 值&page={x}
20 次并html.xpath
为每个页面执行#。
最终,我希望它xpath and item.text
从所有迭代中返回附加的信息列表(20 次)。
目前,它给出了第一次迭代的输出,并打印了 20 次相同的结果。
我是否错过了嵌套 for 循环中的某些内容?
解决方案
代码中没有错误,错误的缩进导致无法生成 URL。我修复了缩进。这是有效的解决方案。
from requests_html import HTMLSession
def get_url(search_text):
session = HTMLSession()
template = 'https://www.amazon.com/s?k={}'
search_term = search_text.replace(' ', '+')
url = template.format(search_term)
url += '&pages={}'
products = []
for x in range(1, 21):
url_inc = url.format(x)
print(url_inc)
r = session.get(url_inc)
r.html.render(sleep=1)
items = r.html.find('h2')
for item in items:
product = item.text
print(product)
products.append(product)
return products
s_list = get_url('Laptop')
输出:-
https://www.amazon.com/s?k=Laptop&pages=1
Laptop 14 Inch, Intel Celeron Processor J3455, Quad-Core, Windows 10, 6GB RAM, 128GB SSD Storage, HD IPS Display, Touch Numeric Keypad, Webcam Baffle, Winbook 140, Space Grey
Acer Aspire 5 Slim Laptop, 15.6 inches Full HD IPS Display, AMD Ryzen 3 3200U, Vega 3 Graphics, 4GB DDR4, 128GB SSD, Backlit Keyboard, Windows 10 in S Mode, A515-43-R19L, Silver
Lenovo IdeaPad 3 14" Laptop, 14.0" FHD 1920 x 1080 Display, AMD Ryzen 5 3500U Processor, 8GB DDR4 RAM, 256GB SSD, AMD Radeon Vega 8 Graphics, Narrow Bezel, Windows 10, 81W0003QUS, Abyss Blue
Acer Nitro 5 Gaming Laptop, 9th Gen Intel Core i5-9300H, NVIDIA GeForce GTX 1650, 15.6" Full HD IPS Display, 8GB DDR4, 256GB NVMe SSD, Wi-Fi 6, Backlit Keyboard, Alexa Built-in, AN515-54-5812
Acer Aspire 5 Slim Laptop, 15.6 inches Full HD IPS Display, AMD Ryzen 3 3200U, Vega 3 Graphics, 4GB DDR4, 128GB SSD, Backlit Keyboard, Windows 10 in S Mode, A515-43-R19L, Silver
HP 15-dy1036nr 10th Gen Intel Core i5-1035G1, 15.6-Inch FHD Laptop, Natural Silver
Acer Chromebook Spin 311 Convertible Laptop, Intel Celeron N4020, 11.6" HD Touch, 4GB LPDDR4, 32GB eMMC, Gigabit Wi-Fi 5, Bluetooth 5.0, Google Chrome, CP311-2H-C679
Acer Predator Helios 300 Gaming Laptop, Intel i7-10750H, NVIDIA GeForce RTX 2060 6GB, 15.6" Full HD 144Hz 3ms IPS Display, 16GB Dual-Channel DDR4, 512GB NVMe SSD, Wi-Fi 6, RGB Keyboard, PH315-53-72XD
ASUS F512JA-AS34 VivoBook 15 Thin and Light Laptop, 15.6” FHD Display, Intel i3-1005G1 CPU, 8GB RAM, 128GB SSD, Backlit Keyboard, Fingerprint, Windows 10 Home in S Mode, Slate Gray
ASUS F512DA-EB51 VivoBook 15 Thin And Light Laptop, 15.6” Full HD, AMD Quad Core R5-3500U CPU, 8GB DDR4 RAM, 256GB PCIe SSD, AMD Radeon Vega 8 Graphics, Windows 10 Home,Slate Gray
ASUS ROG G531GT-BI7N6 15.6" FHD Gaming Laptop Computer, Intel Hexa-Core i7-9750H Up to 4.5GHz, 8GB DDR4, 512GB SSD, NVIDIA GeForce GTX 1650, 802.11ac WiFi, HDMI, USB 3.0, Windows 10
2019 ASUS ROG 15.6" FHD Gaming Laptop Computer, Intel Hexa-Core i7-9750H Up to 4.5GHz, 16GB DDR4, 1TB HDD + 512GB SSD, NVIDIA GeForce GTX 1650, 802.11ac WiFi, HDMI, USB 3.0, Windows 10
Acer Aspire 5 Slim Laptop, 15.6 inches Full HD IPS Display, AMD Ryzen 3 3200U, Vega 3 Graphics, 4GB DDR4, 128GB SSD, Backlit Keyboard, Windows 10 in S Mode, A515-43-R19L, Silver
MSI GL65 Leopard 10SFK-062 15.6" FHD 144Hz 3ms Thin Bezel Gaming Laptop Intel Core i7-10750H RTX2070 16GB 512GB NVMe SSD Win 10
HP 15-dy1036nr 10th Gen Intel Core i5-1035G1, 15.6-Inch FHD Laptop, Natural Silver
iProda Laptop, 14.1 Inch Notebook (Intel Core i3-6157U 2.4GHz, 8GB RAM, 256GB SSD, Windows 10 Professional) with 1080P FHD Display, Lightweight, Best for Work at Home
...
https://www.amazon.com/s?k=Laptop&pages=2 #Now second URL
推荐阅读
- ios - 如何在 swift 标头中传递 OAuth。Twitter Api(获取热门话题标签)
- javascript - Vuetify 中的自定义日期选择器
- laravel - 无法使用 Twilio 发送 GIF
- laravel - 如何使用子目录?
- html - 为什么 textBody.Append 只是将表头附加到第一条记录而不是其他记录?
- aws-lambda - 为什么我的 AWS 安全组允许流量?
- php - 将数组列表键转换为调用数组并替换值
- c++ - 文件处理,eof() 函数
- wpf - 删除 ObservableCollection 中的项目时出现 InvalidCastException
- wcf - c# wcf服务如何消费调用java客户端