我想从分页站点获取一些链接 URL。我正在学习一些教程,因为我对 Selenium(或 Python)不是很熟悉。
无论如何,通过下面的循环我可以从每个页面获取第一个 url,但是我需要获取每个页面的 10 个 url:
browser = webdriver.Firefox()
browser.get("http://www.scba.gov.ar/jurisprudencia/Navbar.asp?Busca=Fallos+Completos&SearchString=Inconstitucionalidad")
time.sleep(5)
x = 0
while (x < 5):
print(browser.find_element_by_xpath('//a[contains(text(),"Completo")]')).get_attribute("href")
browser.find_element_by_xpath("//td[2]/a").click() # Click on next button
time.sleep(5)
x += 1
为了获取每页的所有 url,我尝试使用 find_elements_by_xpath() 代替,但是该函数返回一个列表,并且我收到一条错误消息,指出列表元素没有属性 获取属性。
如果我删除 get 属性部分,我会得到每页 10 行,但不是 url 格式。我得到每个页面的列表,格式如下:
selenium.webdriver.remote.webelement.WebElement object at 0x7f3621cc6dd0>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621cc6d90>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621cc6f90>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621cc6f50>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621cc6ed0>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621c62210>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621c6a110>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621c6a690>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621c75950>, selenium.webdriver.remote.webelement.WebElement object at 0x7f3621c75990>
那么,构建获取 url 然后转到下一页等的循环的正确方法是什么?
感谢任何帮助。
请您参考如下方法:
这里是完整的思路和实现:
- 从页面底部的段落中获取最大页数
- 从当前页面提取链接
- 从下一页循环到最大页数
- 在循环中,点击下一页链接并提取链接
注意事项:
- 代替
time.sleep()比 explicitly wait for the desired element 好得多 - 为了提取最大数量的页面(在本例中为
1910),我在这里使用正则表达式\d+ de (\d+)和 capturing group(\d+)其中\d+匹配一位或多位数字 - 要从多个元素获取
href属性,您只需遍历它们并在每个元素上调用get_attribute()(使用下面的 "list comprehension") - 我不完全确定您要抓取哪些链接,但我假设这些是指向页面上每个 block 底部的文件的链接(指向文件的链接)
代码:
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def extract_data(browser):
links = browser.find_elements_by_xpath('//i[@class="RecordStats"]/a')
return [link.get_attribute('href') for link in links]
browser = webdriver.Firefox()
browser.get("http://www.scba.gov.ar/jurisprudencia/Navbar.asp?Busca=Fallos+Completos&SearchString=Inconstitucionalidad")
# get max pages
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//p[@class='c'][last()]")))
max_pages = int(re.search(r'\d+ de (\d+)', element.text).group(1), re.UNICODE)
# extract from the current (1) page
print "Page 1"
print extract_data(browser)
# loop over the rest of the pages
for page in xrange(2, max_pages + 1):
print "Page %d" % page
next_page = browser.find_element_by_xpath("//table[last()]//td[last()]/a").click()
print extract_data(browser)
print "-----"
打印:
Page 1
[u'http://www.scba.gov.ar/falloscompl/scba/2007/03-16/iniciales.doc', u'http://www.scba.gov.ar/falloscompl/scba/inter/2005/05-26/iniciales.doc', u'http://www.scba.gov.ar/falloscompl/scba/inter/2012/10-31/inicialesb.doc', u'http://www.scba.gov.ar/falloscompl/scba/inter/2006/11-08/i68854.doc', u'http://www.scba.gov.ar/falloscompl/scba/inter/2010/12-15/inicialesrp.doc', u'http://www.scba.gov.ar/falloscompl/scba/2012/07-04/a70660.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/11-24/a69656.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/11-24/a69691.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/11-24/a69693.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/11-24/a69772.doc']
Page 2
[u'http://www.scba.gov.ar/falloscompl/scba/2010/11-24/a69877.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-14/a68974.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a68978.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a68979.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a68982.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a68983.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a69181.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a69588.doc', u'http://www.scba.gov.ar/falloscompl/scba/2004/12-09/p72338.doc', u'http://www.scba.gov.ar/falloscompl/scba/2006/08-16/iniciales.doc']
-----
Page 3
[u'http://www.scba.gov.ar/falloscompl/scba/inter/2010/12-15/rp108872.doc', u'http://www.scba.gov.ar/falloscompl/scba/inter/2007/02-14/i69014-2.doc', u'http://www.scba.gov.ar/falloscompl/scba/2011/05-04/a68445.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a68976.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a68977.doc', u'http://www.scba.gov.ar/falloscompl/scba/2010/07-07/a68981.doc', u'http://www.scba.gov.ar/falloscompl/scba/2004/12-10/iniciales.doc', u'http://www.scba.gov.ar/falloscompl/scba/2014/11-20/iniciales.doc', u'http://www.scba.gov.ar/falloscompl/scba/inter/2013/08-21/a72539.doc', u'http://www.scba.gov.ar/falloscompl/scba/2004/06-23/iniciales.doc']
-----
...
