python - 使用用户代理来防止验证码并启用 javascript
问题描述
我正在抓取一个网站,但是当我从循环中获得 50 次迭代时,它要求我输入验证码或说我没有在浏览器中启用 javascript。顺便说一句,我正在使用 selenium 和 beautifulsoup。有谁知道如何解决这个问题?有没有办法逃避验证码?我只能刮大约一千,因为浏览器由于 capctha 元素而无法获取特定元素。我的python代码
from selenium import webdriver
import urllib
import urllib.request
import string
from bs4 import BeautifulSoup
import mysql.connector
import time
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--enable-javascript")
options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
chrome_path = r"C:\chromedriver.exe"
driver = webdriver.Chrome(chrome_path,options=options)
#total number of pages
pageNumber = 2324
#db connection
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database='property_guru'
)
mycursor = mydb.cursor()
#loop pagination
for i in range(0,pageNumber,1):
pageNumber = str(i)
url = "https://www.propertyguru.com.sg/property-for-sale/"+pageNumber+"?order=desc&property_type=N&property_type_code%5B0%5D=CONDO&property_type_code%5B1%5D=APT&property_type_code%5B2%5D=WALK&property_type_code%5B3%5D=CLUS&property_type_code%5B4%5D=EXCON&sort=date"
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html,'lxml')
list_title = soup.select('.listing-card')
# print(list_title)
# time.sleep(15)
driver.implicitly_wait(10)
for q in list_title:
name = q.find(attrs={'class':'nav-link'})
location = q.find(attrs={'itemprop':'streetAddress'})
listprice = q.find(attrs={'class':'list-price pull-left'})
listAgent = q.find(attrs={'class':'agent-name'})
agentPhone = q.find(attrs={'class':'listing-agent-phone-number'})
var = name.attrs['title']
firstAgentText = listAgent.text
#FINAL VAR
propName = var.replace('For Sale -', '')
propLoc = location.text
propPrice = listprice.text
finalAgent = firstAgentText.replace('Listed by','')
finalPhone = agentPhone.text
print('record inserted')
sql = """INSERT INTO guru_listings (property_name,property_address,property_price,listed_by,contact) VALUES (%s,%s,%s,%s,%s)"""
mycursor.execute(sql,(propName,str(propLoc),str(propPrice),str(finalAgent),str(finalPhone)))
mydb.commit()
# time.sleep(5)
driver.close()
解决方案
推荐阅读
- node.js - 如何在 Nodejs 中使用 websocket 制作乒乓不和谐机器人?
- ruby-on-rails - 如何使用 ruby on rails 获取 firebase 分析?
- javascript - 如何结合 JS 媒体查询和滚动监听?
- jenkins - 如何从 Jenkins 主页中删除凭据部分?
- python - 如何从 iterator.combinations() 创建部分列表?
- python - 我应该在图像数据生成器中进行哪些更改来解决错误?
- pandas - 查找没有无的 groupby/transform 的唯一值
- xamarin.forms - 如果我的 Xamarin Forms Android 只有一个图标,是否需要创建启动画面?
- json - 我怎样才能把这个“\/\/cdn.apixu.com\/weather\/64x64\/day\/116.png”放在一个字符串里面,我对“\/\/”有问题
- android - 下载管理器:状态 HTTP 数据错误:无法从移动设备 IP 地址 android 连接到 Web 服务器