最近想爬取一个日文网站的搜索结果,
https://shop.keionet.com/search/index.html#reorder
不知道为什么抓取不到数据,显示的都是0条数据
python来抓取
请指教一下,谢谢
from bs4 import BeautifulSoup
import requests
def main():
#
url_items = "https://shop.keionet.com/search/index.html"
# 获取加密后的数据
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
# "Cookie": "D94FECC2CEBF0B0993D4F1A703477984.koec-a; AUI=1b82b14046cbc43a56750512ada793b81ebd06f45311c1b8f29390425017beee;",
"Cookie": "JSESSIONID=653F73FC30ACAE0F025439D2572B08AB.koec-a;",
# "Cookie": "JSESSIONID=4276D482EBB4B4573F8E7134CD5C1408.koec-a",
# "Cookie": cookies_str,
}
session = requests.session()
# session.get(url=url_items,headers=headers)
# print("直接获取cookie:", session.cookies, type(session.cookies))
# print(session.cookies.get("JSESSIONID"))
#
# cookies_str="JSESSIONID="+session.cookies.get("JSESSIONID")
# print(cookies_str)
# #
# # session = requests.session()
# # session.get(url_items)
# html_set_cookie = requests.utils.dict_from_cookiejar(session.cookies)
# print(html_set_cookie)
# requests.getでHTMLを取得
item_data = {
"indexForm:keyword": "AA",
"indexForm/view/front/search/index.html": "indexForm",
}
r_post = session.post(url_items, headers=headers, data=item_data)
# print(r_post.text)
# print(r_post.cookies) # 看cookie
soup = BeautifulSoup(r_post.content, "html.parser")
# print(soup)
wc = soup.find(class_="itemGroupThumbs")
print(wc)
#
# print("直接获取cookie:", session.cookies, type(session.cookies))
# print(session.cookies.get("JSESSIONID"))
# cookies_str = "JSESSIONID=" + session.cookies.get("JSESSIONID")
# print(cookies_str)
# wc_name=wc.find(class_="name")
# print(wc_name)
# wc_price = wc.find(class_="retail_price")
# print(wc_price)
# wc_icon = wc.find(class_="icon")
# print(wc_icon)
# ws = [i.strip() for i in wc.text.splitlines()]
# print(ws)
# print(r_post.text)
# return (r_post)
# col1 = r_post.get('AuthenticationResult')
# col2 = col1.get('IdToken')
# print(col2)
if __name__ == "__main__":
main()
您可能没有正确地解析搜索结果。如果这是问题所在,您需要仔细查看网页源代码,并确定正确的搜索结果格式。
不知道你这个问题是否已经解决, 如果还没有解决的话:我尝试访问 https://shop.keionet.com/search/index.html#reorder 网站并查看搜索结果,发现该网站使用JavaScript生成了搜索结果,因此,我们需要使用Selenium模块生成浏览器实例模拟人的操作来获取该网站的搜索结果。
首先,我们需要安装Selenium模块:
!pip install selenium
接下来,我们需要下载Chrome浏览器的驱动程序。你可以在 http://chromedriver.chromium.org/downloads 下载Chrome浏览器的驱动程序。请下载与你所使用的Chrome浏览器版本相对应的驱动程序,并将其放置在你的电脑上。
然后,我们就可以使用以下代码来获取搜索结果了:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument('--headless') # 无头模式,不需要启动浏览器
options.add_argument('--disable-gpu') # 禁用GPU加速,提高稳定性
driver = webdriver.Chrome('path/to/chromedriver', options=options) # 实例化浏览器对象
driver.get('https://shop.keionet.com/search/index.html#reorder') # 访问网站
# 找到搜索框并输入搜索关键字"面膜"
search_box = driver.find_element_by_css_selector('#word')
search_box.send_keys('面膜')
# 找到搜索按钮并点击
search_button = driver.find_element_by_css_selector('.submit_btn')
search_button.click()
# 等待1秒
time.sleep(1)
# 找到搜索结果
result = driver.find_element_by_css_selector('.item')
# 输出搜索结果
print(result.text)
# 关闭浏览器
driver.quit()
注意,代码中的path/to/chromedriver
需要替换为你自己的Chrome浏览器驱动程序的路径。
另外,如果你需要爬取多页的搜索结果,可以使用循环来实现:
from selenium.common.exceptions import NoSuchElementException
page_num = 1 # 设置起始页
while True:
print('正在获取第{}页的数据...'.format(page_num))
# ...
# 如果找不到下一页按钮,则说明已经爬取到了最后一页,退出循环
try:
next_button = driver.find_element_by_css_selector('.next')
except NoSuchElementException:
print('已经到了最后一页')
break
# 找到下一页按钮并点击
next_button.click()
# 等待1秒
time.sleep(1)
page_num += 1 # 页码加1
# 关闭浏览器
driver.quit()
这样,我们就可以使用Selenium模块来爬取 https://shop.keionet.com/search/index.html#reorder 网站的搜索结果了。
注意:爬虫的速度不要过快,以免给对方服务器带来过大的压力,最好加上适当的延时。同时,请尊重网站的规定,不要进行恶意爬取、扰乱对方正常的网站运营。