【发布时间】:2017-12-17 12:12:00
【问题描述】:
如何编辑驱动程序当前网址,以便链接来自:
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
【问题讨论】:
-
你只是想将
#添加到driver.current_url返回的字符串中,对吧? -
@Andersson 正确。从centrebet.com/Sports/12313443 到:centrebet.com/#Sports/12313443
标签: python regex selenium url-rewriting urlparse