2016-09-28 52 views
4

感谢您抽出宝贵看看我的问题。我想知道是否有任何方式拉从这个文本数据sitekey ...这里是链接到页面https://e-com.secure.force.com/adidasUSContact/使用Python请求和美丽的汤拉文

<div class="g-recaptcha" data-sitekey="6LfI8hoTAAAAAMax5_MTl3N-5bDxVNdQ6Gx6BcKX" data-type="image" id="ncaptchaRecaptchaId"><div style="width: 304px; height: 78px;"><div><iframe src="https://www.google.com/recaptcha/api2/anchor?k=6LfI8hoTAAAAAMax5_MTl3N-5bDxVNdQ6Gx6BcKX&amp;co=aHR0cHM6Ly9lLWNvbS5zZWN1cmUuZm9yY2UuY29tOjQ0Mw..&amp;hl=en&amp;type=image&amp;v=r20160921114513&amp;size=normal&amp;cb=ei2ddcb6rl03" title="recaptcha widget" width="304" height="78" role="presentation" frameborder="0" scrolling="no" name="undefined"></iframe></div><textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none; display: none; "></t 

这里是我当前的代码

import requests 
from bs4 import BeautifulSoup 

headers = { 
    'Host' : 'e-com.secure.force.com', 
    'Connection' : 'keep-alive', 
    'Upgrade-Insecure-Requests' : '1', 
    'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64)', 
    'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 
    'Accept-Encoding' : 'gzip, deflate, sdch', 
    'Accept-Language' : 'en-US,en;q=0.8' 
} 
url = 'https://e-com.secure.force.com/adidasUSContact/' 
r = requests.get(url, headers=headers) 
soup = BeautifulSoup(r, 'html.parser') 
c = soup.find_all('div', attrs={"class": "data-sitekey"}) 
print c 
+0

哪里是你的代码,使远吗?有百分之百的方法,但很高兴看到你的努力到目前为止。 –

+1

@PadraicCunningham更新 –

+0

@jonrsharpe更新 –

回答

6

好现在我们的代码,它是那样简单:

import requests 
from bs4 import BeautifulSoup 


soup = BeautifulSoup(requests.get("https://e-com.secure.force.com/adidasUSContact/").content, "html.parser") 

key = soup.select_one("#ncaptchaRecaptchaId")["data-sitekey"] 

数据sitekey属性不是 a css类,所以你只需要从元素中提取它,你可以通过它找到它的元素id如上。

你也可以使用类名:

# css selector 
key = soup.select_one("div.g-recaptcha")["data-sitekey"] 
# regular find using class name 
key = soup.find("div",class_="g-recaptcha")["data-sitekey"] 
+0

好的,让我试试这个。 –

+0

@Tonysanchez,肯定会工作;) –

+0

工作,但其现在告诉我一些关于标记 –