from selenium import webdriver
import time
driver = webdriver.Chrome[executable_path=r'C:\Users\gkhat\Downloads\chromedriver.exe']
driver.get['//www.allrecipes.com/recipes/233/world-cuisine/asian/indian/']
card_titles = driver.find_elements_by_class_name['card__detailsContainer']
button = driver.find_element_by_id['category-page-list-related-load-more-button']
for card_title in card_titles:
rname = card_title.find_element_by_class_name['card__title'].text
print[rname]
time.sleep[3]
driver.execute_script["arguments[0].scrollIntoView[true];", button]
driver.execute_script["arguments[0].click[];", button]
time.sleep[3]
driver.quit[]
Trang web tải các thẻ thực phẩm sau khi nhấp vào nút "Tải thêm", mã trên cạo tiêu đề công thức tôi muốn nó tiếp tục cạo tiêu đề ngay cả sau khi nhấp vào nút tải thêm. Tôi đã thử đi đến tab Mạng nhấp vào XHR nhưng không có yêu cầu nào hiển thị JSON. Tôi nên làm gì?
Hỏi ngày 15 tháng 8 năm 2021 lúc 14:26Aug 15, 2021 at 14:26
2
Tôi đã thử dưới đây mã cho điều đó. Nó hoạt động, nhưng tôi không chắc đây có phải là cách tốt nhất để làm điều đó không. FYI tôi đã xử lý những cửa sổ bật lên đó cho email
theo cách thủ công. Bạn cần tìm cách xử lý chúng.
from selenium import webdriver
import time
from selenium.common.exceptions import StaleElementReferenceException
driver = webdriver.Chrome[executable_path="path"]
driver.maximize_window[]
driver.implicitly_wait[10]
driver.get["//www.allrecipes.com/recipes/233/world-cuisine/asian/indian/"]
receipes = driver.find_elements_by_class_name["card__detailsContainer"]
for rec in receipes:
name = rec.find_element_by_tag_name["h3"].get_attribute["innerText"]
print[name]
loadmore = driver.find_element_by_id["category-page-list-related-load-more-button"]
j = 0
try:
while loadmore.is_displayed[]:
loadmore.click[]
time.sleep[5]
lrec = driver.find_elements_by_class_name["recipeCard__detailsContainer"]
newlist = lrec[j:]
for rec in newlist:
name = rec.find_element_by_tag_name["h3"].get_attribute["innerText"]
print[name]
j = len[lrec]+1
time.sleep[5]
except StaleElementReferenceException:
pass
driver.quit[]
Đã trả lời ngày 15 tháng 8 năm 2021 lúc 15:44Aug 15, 2021 at 15:44
pmadhupmadhupmadhu
3.2132 Huy hiệu vàng10 Huy hiệu bạc22 Huy hiệu đồng2 gold badges10 silver badges22 bronze badges
0
Trên thực tế, có một JSON trả về dữ liệu. Tuy nhiên, JSON trả lại nó trong HTML, vì vậy chỉ cần phân tích cú pháp đó.
Lưu ý: Bạn có thể thay đổi kích thước chunk để bạn có thể nhận được nhiều hơn 24 mục cho mỗi "trang"
import requests
from bs4 import BeautifulSoup
size = 24
page = 0
hasNext = True
while hasNext == True:
page +=1
print['\tPage: %s' %page]
url = '//www.allrecipes.com/element-api/content-proxy/aggregate-load-more?sourceFilter%5B%5D=alrcom&id=cms%2Fonecms_posts_alrcom_2007692&excludeIds%5B%5D=cms%2Fallrecipes_recipe_alrcom_142967&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_231026&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_247233&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_246179&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_256599&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_247204&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_34591&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_245131&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_220560&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_212721&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_236563&excludeIds%5B%5D=cms%2Fallrecipes_recipe_alrcom_14565&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_8189766&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_8188886&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_8189135&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_2052087&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_7986932&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_2040338&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_280310&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_142967&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_14565&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_228957&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_46822&excludeIds%5B%5D=cms%2Fonecms_posts_alrcom_72349&page={page}&orderBy=Popularity30Days&docTypeFilter%5B%5D=content-type-recipe&docTypeFilter%5B%5D=content-type-gallery&size={size}&pagesize={size}&x-ssst=iTv629LHnNxfbQ1iVslBTZJTH69zVWEa&variant=food'.format[size=size, page=page]
jsonData = requests.get[url].json[]
hasNext = jsonData['hasNext']
soup = BeautifulSoup[jsonData['html'], 'html.parser']
cardTitles = soup.find_all['h3',{'class':'recipeCard__title'}]
for title in cardTitles:
print[title.text.strip[]]
Đã trả lời ngày 16 tháng 8 năm 2021 lúc 11:32Aug 16, 2021 at 11:32
chitown88chitown88chitown88
26.8K4 Huy hiệu vàng27 Huy hiệu bạc58 Huy hiệu Đồng4 gold badges27 silver badges58 bronze badges