繁体   English   中英

来自driver.URL的URL列表

[英]URL list from driver.current url

我如何编辑驱动程序的当前URL,以便链接从http://centrebet.com/Sports/12313443变为以下链接: http://centrebet.com/Sports/12313443 : http://centrebet.com/#Sports/12313443

http://centrebet.com//Sports/是不变的

我发现了很多带有静态链接的示例,但是我对如何使用一系列当前URL感到困惑。

码:

driver = webdriver.Chrome()

url = "http://centrebet.com/"
driver.get(url)

def page_counter():
  for x in range(1000):
      yield x

count = page_counter()
driver.get(url)
sports = driver.find_element_by_id("accordionMenu1_ulSports")
links = [url + link.get_attribute("onclick").replace("menulink('", "").replace("')", "") for link in sports.find_elements_by_xpath('//ul[@id="accordionMenu1_ulSports"]//li//ul//li//ul//li//a[starts-with(@onclick, "menulink")]')]



links = dict((next(count) + 1, e) for e in links)

desc_links = collections.OrderedDict(sorted(links.items(), reverse=True))
for key, value in desc_links.items():
    try:
        driver.get(value)
        ...



        langs4 = driver.find_elements_by_css_selector("tbody > tr:nth-child(2) > td > table > tbody > tr > td > table > tbody > tr > td:nth-child(2) > table > tbody > tr:nth-child(3) > td > table > tbody > tr > td > table > tbody > tr > td:nth-child(1) > div > div")
        langs4_text = []
        for lang in langs4:
            # print(lang.text)
            langs4_text.append(lang.text)

        url1 = driver.current_url

try:
    import urlparse
    from urllib import urlencode
except: 
    import urllib.parse as urlparse
    from urllib.parse import urlencode

url = "http://centrebet.com/"
params = {'#':'#','Sports':'Sports'}

url_parts = list(urlparse.urlparse(url))
query = dict(urlparse.parse_qsl(url_parts[4]))
query.update(params)

url_parts[4] = urlencode(query)

print(urlparse.urlunparse(url_parts))







        with open('C:\\O131.csv', 'a', newline='', encoding="utf-8") as outfile:
            writer = csv.writer(outfile)
            for row in zip(langs4_text):
                writer.writerow(row + (url1,))
    except TimeoutException as ex:
        pass

仍然不确定我确切地知道您想做什么。 但是,如果仅在URL中添加# ,则可以简单地应用以下解决方案:

url = "http://centrebet.com/"
current_url = driver.current_url # http://centrebet.com/Sports/12313443
new_url = url + "#".join(current_url.split(url)) # http://centrebet.com/#Sports/12313443 

要么

url = "http://centrebet.com/"
current_url = driver.current_url # http://centrebet.com/Sports/12313443
new_url = current_url.replace(url, url + "#") # http://centrebet.com/#Sports/12313443 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM