我使用的是Python 2.7和Selenium 2.44.
我想在Selenium WD中自动执行拖放操作,但根据其他相关帖子,Selenium尚不支持HTML5中的操作.有没有办法在Python中模拟拖放?
这是我试过的代码:
driver = webdriver.Firefox()
driver.get("http://html5demos.com/drag")
target = driver.find_element_by_id("one")
source = driver.find_element_by_id("bin")
actionChains = ActionChains(driver)
actionChains.drag_and_drop(target, source).perform()
Run Code Online (Sandbox Code Playgroud)
它不起作用.
我正在为不同的新闻媒体创建一个网络刮板,对于Nytimes和Guardian来说,它很容易,因为它们有自己的API.
现在,我想从这份报纸GulfTimes.com中获取结果.他们没有在他们的网站上提供高级搜索,所以我使用谷歌新闻.但是,Google新闻Api已被弃用.我想要的是从高级搜索中检索结果的数量,如关键字="埃及"和begin_date ="10/02/2011"和end_date ="10/05/2011".
这在Google新闻用户界面中是可行的,只需将源代码设置为"海湾时代"以及相应的查询和日期,并简单地手动计算结果数量,但当我尝试使用python执行此操作时,我得到403错误,这是可以理解的.
我对如何做到这一点有任何想法吗?或者除谷歌新闻之外还有其他服务可以让我这样做吗?请记住,我会一次发出近500个请求.
import json
import urllib2
import cookielib
import re
from bs4 import BeautifulSoup
def run():
Query = "Egypt"
Month = "3"
FromDay = "2"
ToDay = "4"
Year = "13"
url='https://www.google.com/search?pz=1&cf=all&ned=us&hl=en&tbm=nws&gl=us&as_q='+Query+'&as_occt=any&as_drrb=b&as_mindate='+Month+'%2F'+FromDay+'%2F'+Year+'&as_maxdate='+Month+'%2F'+ToDay+'%2F'+Year+'&tbs=cdr%3A1%2Ccd_min%3A3%2F1%2F13%2Ccd_max%3A3%2F2%2F13&as_nsrc=Gulf%20Times&authuser=0'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
request = urllib2.Request(url)
response = opener.open(request)
htmlFile = BeautifulSoup(response)
print htmlFile
run()
Run Code Online (Sandbox Code Playgroud) 我目前正在使用Flask Web框架在Python中编写Web应用程序.我真的习惯于把所有东西放在一个文件中,不像我看到的其他项目,它们有不同的类,视图和东西目录.但是,Flask示例只是将所有内容都放入一个文件中,这就是我的目标.
在单个文件中编写整个Web应用程序是否存在任何风险或问题,或者更好地将我的函数和类分散到单独的文件中?
我有一个xml我正在解析,进行一些更改并保存到一个新文件.它有<?xml version="1.0" encoding="utf-8" standalone="yes"?>我想保留的声明.当我保存我的新文件时,我失去了standalone="yes"一点.我该如何保管?这是我的代码:
templateXml = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<package>
<provider>Some Data</provider>
<studio_display_name>Some Other Data</studio_display_name>
</package>"""
from lxml import etree
tree = etree.fromstring(templateXml)
xmlFileOut = '/Users/User1/Desktop/Python/Done.xml'
with open(xmlFileOut, "w") as f:
f.write(etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8'))
Run Code Online (Sandbox Code Playgroud) 我尝试使用lxml在Python中解析XML文件,如下所示:
objectify.parse(xmlPath, parserWithSchema)
Run Code Online (Sandbox Code Playgroud)
但XML文件可能包含奇怪的地方的评论:
<root>
<text>Sam<!--comment-->ple text</text>
<!--comment-->
<float>1.2<!--comment-->3456</float>
</root>
Run Code Online (Sandbox Code Playgroud)
这是一种在解析之前不加载或删除注释的方法吗?
我不确定我是否完全理解以下正则表达式搜索的内容:
>>> import re
>>> template = re.compile("(\w+)+\.")
>>> target = "a" * 30
>>> template.search(target)
Run Code Online (Sandbox Code Playgroud)
search()呼叫需要几分钟才能完成,CPU使用率达到100%.对于2.7.5和3.3.3 Python版本,该行为都是可重现的.
有趣的事实是,如果字符串的长度小于20-25个字符,那么很快就会search()返回.
怎么了?
给定一个更高阶函数,它将多个函数作为参数,该函数如何将关键字参数传递给函数参数?
例
def eat(food='eggs', how_much=1):
print(food * how_much)
def parrot_is(state='dead'):
print("This parrot is %s." % state)
def skit(*lines, **kwargs):
for line in lines:
line(**kwargs)
skit(eat, parrot_is) # eggs \n This parrot is dead.
skit(eat, parrot_is, food='spam', how_much=50, state='an ex-parrot') # error
Run Code Online (Sandbox Code Playgroud)
state不是关键字arg,eat那么skit如何才能传递与其调用的函数相关的关键字args?
我试图用类来抓住div:'产品'.问题是,一些带有"产品"类的div也有"产品小"这个类.所以当我使用时xpath('//div[@class='product']'),它只捕获一个类而不是多个的div.我怎么能用scrapy做到这一点?
例:
<div class='product'><div class='product product-small'>是否有可能只从父元素获取文本而不是Selenium中的子元素?
示例:假设我有以下代码:
<div class="linksSection>
<a href="https://www.google.com/" id="google">Google Link
<span class="helpText">This link will take you to Google's home page.</span>
</a>
...
</div>
Run Code Online (Sandbox Code Playgroud)
在C#(或任何语言)中,我将:
string linktext = driver.FindElement(By.CssSelector(".linksSection > a#google")).Text;
Assert.AreEqual(linkText, "Google Link", "Google Link fails text test.");
Run Code Online (Sandbox Code Playgroud)
但是,linktext将具有"谷歌链接此链接将带您到谷歌的主页."
没有做一堆字符串操作(比如获取所有子节点的文本并从父节点的结果文本中减去它),有没有办法从父元素中获取文本?
python ×8
lxml ×2
selenium ×2
web-scraping ×2
xml ×2
xml-parsing ×2
c# ×1
coding-style ×1
comments ×1
flask ×1
function ×1
google-news ×1
html ×1
html5 ×1
javascript ×1
jshint ×1
kwargs ×1
parsing ×1
performance ×1
regex ×1
scrapy ×1
standards ×1
string ×1
xpath ×1