ตอนนี้ผมได้ทำ selenium Webdriver ในการสร้างบอทขึ้นมาเพื่อที่จะให้บอทนั้นสามารถเข้าไปคลิกโหวตบทความ ให้กับเว็บ https://www.mindphp.com/ แต่เมื่อบอทเริ่มทำงานคลิกโหวตไปเรื่อย ๆ หน้าเว็บก็จะโหลดนานในการเข้าไปที่บทความถัดไป แล้วก็จะมี Error 502 Bad Gateway ขึ้นมาตัวอย่างเมื่อทำการรันแล้วเกิด Error ขึ้นด้านล่าง
คำสั่งที่ผมใช้ในการวนลูปเพื่อให้บอทเข้าไปยังบทความต่าง ๆมีดังนี้ครับ
โค้ด: เลือกทั้งหมด
# หา Element โดยใช้ Xpath
title_list = driver.find_elements_by_xpath(
"//div[@class='category-list']//table[@class='category "
"table table-striped table-bordered table-hover']/tbody/tr//a[@href]")
# นำจำนวนบทความที่มีในหน้านั้น
countRow = len(driver.find_elements_by_xpath(
"//div[@class='category-list']//table[@class='category table table-striped table-bordered table-hover']/tbody/tr"))
print(countRow)
# นำ Session และ Element มาแปลงเป็นลิ้งค์
link = []
for web_link in title_list:
link.append(web_link.get_attribute("href"))
print(link)
check = driver.page_source
footer = driver.find_element_by_xpath("//footer/div")
header = driver.find_element_by_xpath("//body/div/div/div")
# ทำการวน loop เพื่อคลิกล้งค์
for web_link in link:
print(web_link)
time.sleep(2)
driver.get(web_link)
print('-----Open Link------')
if "header" in check:
print("หน้าเว็บสมบูรณ์")
else:
print("หน้าเว็บไม่สมบูรณ์ ขาด Header")
if "footer" in check:
print("หน้าเว็บสมบูรณ์")
else:
print("หน้าเว็บไม่สมบูรณ์ ขาด Footer")
time.sleep(1)
driver.find_element_by_xpath("//input[@value='เรต']").click()
time.sleep(1)
driver.get("https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-"
"%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%"
"E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B.html")
จะเห็นได้ว่าบอทนั้นได้คลิกเข้าไปในบทความต่าง ๆ แต่เมื่อผ่านไปสักพักก็จะมีการโหลดหน้าเว็บเพื่อเข้าบทความนานขึ้น จากนั้นก็มี Error ขึ้น ครับ อยากรู้ว่ามีวิธีแก้ไขกับ Error ยังไงบ้างครับ