- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试从网站抓取数据,但问题是有加载更多按钮来查看接下来的 50 条记录,同样我也必须单击直到记录结束。
我只能获取 50 个姓名和地址。需要获取所有内容直到加载更多结束。
对于动态点击按钮,我使用 selenium 和 python。
我想查找城市中所有零售商的名称、地址和联系电话
我的尝试:
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
url = "https://www.test.in/chemists/medical-store/gujarat/surat"
browser = webdriver.Chrome()
browser.get(url)
time.sleep(1)
html = browser.page_source
soup = BeautifulSoup(html, "lxml")
try:
for row in soup.find_all("div", {"class":"listing "}):
#print(row.get_text())
name = row.h3.a.string
address = row.p.get_text()
#contactnumber = need to find (can view after click on retailer name )
print(name)
print(address)
print(contactnumber)
button = browser.find_element_by_id("loadmore")
button.click()
except TimeoutException as ex:
isrunning = 0
#browser.close()
#browser.quit()
最佳答案
如果您检查点击 load more
时进行的网络调用您可以看到这是一个 POST 请求,参数为城市、州和页码。因此,您可以使用普通的请求模块来代替在 Selnium 中加载脚本。例如,当您迭代页面时,此函数将为您执行加载更多函数。
def hitter(page):
url = "https://www.healthfrog.in/importlisting.html"
payload = "page="+str(page)+"&mcatid=chemists&keyword=medical-store&state=gujarat&city=surat"
headers = {
'content-type': "application/x-www-form-urlencoded",
'connection': "keep-alive",
'cache-control': "no-cache",
'postman-token': "d4baf979-343a-46e6-f53b-2f003d04da82"
}
response = requests.request("POST", url, data=payload, headers=headers)
return response.text
上面的函数为您获取包含名称和地址的页面的 html。现在您可以遍历页面,直到找到不返回任何内容的页面。例如,如果尝试使用卡纳塔克邦和迈 Solr 城市,您会注意到第三页和第四页之间的差异。这会告诉您必须在哪里停止。
要获取电话号码,您可以从<h3>
请求html。批量列表响应(之前的响应)的标签。示例 html:
<div class="listing">
<h3>
<a href="https://www.healthfrog.in/chemists/sunny-medical-store-surat-v8vcr3alr.html">Sunny Medical Store</a>
</h3>
<p>
<i class="fa fa-map-marker"></i>155 - Shiv Shakti Society, Punagam, , Surat, Gujarat- 394210,India
</p>
</div>
您需要解析 html 并找出电话号码在哪里,然后才能填充它。您可以使用以下方式请求此示例:
html = requests.get('https://www.healthfrog.in/chemists/sunny-medical-store-surat-v8vcr3alr.html').text
您现在可以使用 beautifulSoup
解析 html就像您之前所做的那样。
使用请求而不是 Selenium 来完成此操作有很多优点,您不需要每次需要电话号码时打开和关闭多个窗口,并且可以避免每次点击加载更多时元素过期。它也快得多。
<小时/>请注意:如果您进行此类抓取,请遵守网站制定的规则。不要因发送太多请求而使其崩溃。
编辑:工作刮刀。
import requests, time, re
from bs4 import BeautifulSoup
def hitter(page, state="Gujarat", city="Surat"):
url = "https://www.healthfrog.in/importlisting.html"
payload = "page="+str(page)+"&mcatid=chemists&keyword=medical-store&state="+state+"&city="+city
headers = {
'content-type': "application/x-www-form-urlencoded",
'connection': "keep-alive",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers)
return response.text
def getPhoneNo(link):
time.sleep(3)
soup1 = BeautifulSoup(requests.get(link).text, "html.parser")
f = soup1.find('i', class_='fa fa-mobile').next_element
try:
phone = re.search(r'(\d{10})', f).group(1)
except AttributeError:
phone = None
return phone
def getChemists(soup):
stores = []
for row in soup.find_all("div", {"class":"listing"}):
print(row)
dummy = {
'name': row.h3.string,
'address': row.p.get_text(),
'phone': getPhoneNo(row.h3.a.get_attribute_list('href')[0])
}
print(dummy)
stores.append(dummy)
return stores
if __name__ == '__main__':
page, chemists = 1, []
city, state = 'Gulbarga', 'Karnataka'
html = hitter(page, state, city)
condition = not re.match(r'\A\s*\Z', html)
while(condition):
soup = BeautifulSoup(html, 'html.parser')
chemists += getChemists(soup)
page += 1
html = hitter(page, state, city)
condition = not re.match(r'\A\s*\Z', html)
print(chemists)
关于python - 使用 python 和 selenium 进行网络爬行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46280636/
Selenium IDE、Selenium RC 和 Selenium WebDriver 有什么区别;我们可以在什么样的项目中使用它们?任何建议将不胜感激。 最佳答案 Selenium IDE 是一
我的 Selenium 服务器在远程服务器上运行。我从我的本地 PC 启动我的 Selenium 脚本,它从网站获取数据。 例如,我的 Selenium 脚本执行这段 JS 代码: JSON.stri
Selenium 中“//div[.//a[text()='SELENIUM']]”和“//div[//a[text()='SELENIUM']]”有什么区别xpath。 有人可以澄清我在 xpath
我正在创建自动冒烟测试。我读到在单元测试中使用多个断言不是一个好的做法,这条规则是否也适用于使用 selenium 的 webdriver 测试? 在我的冒烟测试中,有时我会使用 20 多个断言来验证
我在一个变量中存储了一个值,在另一个变量中存储了第二个值,现在我想将这两个数字相加。我无法做到这一点,我尝试过下面的代码,但它不起作用 store 6 w sto
Selenium 中的回车键和回车键有什么区别? This related SO answer并且提供的链接说明它们是不同的。我还注意到,在使用 Firefox 24.2 时,回车键将发送一个 HTM
以下是我遇到异常的详细信息: 当我使用以下命令启动节点时,出现如下错误: F:\SeleniumGrid\Jars>java -jar selenium-server-standalone-3.0.0
我是 的新手 Selenium 我对版本号有点困惑。 Selenium 2.0 2011年发布。我刚刚下载了 Selenium IDE Firefox 扩展,版本为 1.7.2 .是否还有 IDE 的
我正在使用 Selenium 运行Codeception 2。我可以看到 Selenium 打开了浏览器并运行了测试。然后,我从代码接收中得到一个错误,即存在失败的断言。 我知道有一个HTML文件可以
Closed. This question needs to be more focused。它当前不接受答案。 想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。
我想关闭弹出窗口(已知的窗口名称),然后返回到原始窗口。 我该怎么办? 如果我无法获得窗口中关闭按钮的常量。那么有没有达到目标的一般行为? 最佳答案 你有没有尝试过: selenium.Close()
我正在用webdriver做一个测试机器人。我有一个场景,它单击一个按钮,打开一个新窗口,并且它通过特定的xpath搜索元素,但是有时没有这样的元素,因为可以将其禁用,并且出现此错误:org.open
我是第一次使用Selenium,对这些选项不知所措。我在Firefox中使用IDE。 当我的页面加载时,它随后通过JSONP请求获取值,并在其中填充选择中的选项。 我如何让Selenium等待选择中的
我开始使用nightwatch.js编写e2e测试,我注意到我想在目标浏览器的控制台(开发人员工具)中手动检查一些错误。但总是在我打开开发者控制台时,浏览器会自动关闭它。这是selenium还是nig
我正在尝试使用以下方式刮除Glassdoor的评论: https://github.com/MatthewChatham/glassdoor-review-scraper 但是我得到了错误并且不知道如
背景 我设置了一个Selenium Grid项目,以在两种不同的浏览器Chrome和Firefox中执行测试。我正在使用Gradle执行测试。该测试将成功执行两次,一次按预期在Chrome中执行,一次
当测试失败时,运行 selenium 测试的浏览器将关闭。这在尝试调试时没有帮助。我知道我可以在失败时选择屏幕截图,但如果没有整个上下文,这并没有帮助。在浏览器仍然可用的情况下,我可以回击并检查发生了
使用 Selenium Web 驱动程序而不是 Selenium RC 启动新的测试框架是个好主意吗?对于 Selenium Web 驱动程序,并非所有 Selenium 方法都已实现。那么使用 Se
我使用 selenium 页面对象模型来定义所有页面元素。我对元素命名所遵循的命名约定不太相信,并且感觉太长了。请对此提出建议。 @FindBy(xpath = "//tbody[@id='tabvi
有一个带有按钮的 html 页面,我的 Selenium 测试正在测试,当单击按钮时,会执行一个操作。 问题是,看起来点击发生在 javascript 执行之前 - 在处理程序绑定(bind)到页面之
我是一名优秀的程序员,十分优秀!