我在vba中结合IE编写了一个脚本,点击网页上地图上的一些点.单击一个点时,会弹出一个包含相关信息的小框.
我想解析每个盒子的内容.可以使用类名找到该框的内容contentPane
.但是,这里主要关注的是通过单击这些点来生成每个框.当一个框出现时,它看起来如下图所示.
这是我到目前为止尝试过的脚本:
Sub HitDotOnAMap()
Const Url As String = "https://www.arcgis.com/apps/Embed/index.html?webmap=4712740e6d6747d18cffc6a5fa5988f8&extent=-141.1354,10.7295,-49.7292,57.6712&zoom=true&scale=true&search=true&searchextent=true&details=true&legend=true&active_panel=details&basemap_gallery=true&disable_scroll=true&theme=light"
Dim IE As New InternetExplorer, HTML As HTMLDocument
Dim post As Object, I&
With IE
.Visible = True
.navigate Url
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
End With
Application.Wait Now + TimeValue("00:0:07") ''the following line zooms in the slider
HTML.querySelector("#mapDiv_zoom_slider .esriSimpleSliderIncrementButton").Click
Application.Wait Now + TimeValue("00:0:04")
With HTML.querySelectorAll("[id^='NWQMC_VM_directory_'] circle")
For I = 0 To .Length - 1
.item(I).Focus
.item(I).Click …
Run Code Online (Sandbox Code Playgroud) 我用Python编写了一个与selenium相关的脚本,点击地图中的每个标志.但是,当我执行我的脚本时,它会timeout exception
在到达此行时抛出错误wait.until(EC.staleness_of(item))
.
在点击该行之前,该脚本应该已经点击过一次,但它不能?如何循环点击该地图中的所有标志?
到目前为止这是我的代码(也许,我正在尝试使用错误的选择器):
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
link = "https://www.findapetwash.com/"
driver = webdriver.Chrome()
driver.get(link)
wait = WebDriverWait(driver, 15)
for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#map .gm-style"))):
item.click()
wait.until(EC.staleness_of(item))
driver.quit()
Run Code Online (Sandbox Code Playgroud)
在该地图上可见的标志如:
发布脚本:我知道这是他们
https://www.findapetwash.com/api/locations/getAll/
用来获取JSON内容的API ,但我想坚持使用Selenium方式.谢谢.
我在vba中编写了一个刮刀来解析一些来自torrent网站的电影信息.我使用IE
并queryselector
完成任务.当我执行我的代码时,它会解析所有内容以及弹出错误.似乎错误出现了,而不是继续.如果我取消错误框,那么我可以看到结果.我在下面上传了两张图片,向您展示我遇到的错误.如何在没有任何错误的情况下成功执行代码?提前致谢.
这是完整的代码:
Sub Torrent_Data()
Dim IE As New InternetExplorer, html As HTMLDocument
Dim post As Object
With IE
.Visible = False
.navigate "https://yts.am/browse-movies"
Do While .readyState <> READYSTATE_COMPLETE: Loop
Set html = .Document
End With
For Each post In html.querySelectorAll(".browse-movie-bottom")
Row = Row + 1: Cells(Row, 1) = post.queryselector(".browse-movie-title").innerText
Cells(Row, 2) = post.queryselector(".browse-movie-year").innerText
Next post
IE.Quit
End Sub
Run Code Online (Sandbox Code Playgroud)
我遇到的错误:
这两个错误同时出现.我正在使用Internet Explorer 11.
另一方面,如果我尝试如下,它会成功地带来结果,没有任何问题.
Sub Torrent_Data()
Dim IE As New InternetExplorer, html As HTMLDocument
Dim post As …
Run Code Online (Sandbox Code Playgroud) 我在python中使用BeautifulSoup库编写了一个刮刀来解析遍历网站不同页面的所有名称.如果不是一个不同分页的网址,我可以管理它,这意味着一些网址有一些不分页,因为内容很少.
我的问题是:我怎么能设法在一个函数中编译它们来处理它们是否有分页?
我最初的尝试(它只能解析每个网址首页的内容):
import requests
from bs4 import BeautifulSoup
urls = {
'https://www.mobilehome.net/mobile-home-park-directory/maine/all',
'https://www.mobilehome.net/mobile-home-park-directory/rhode-island/all',
'https://www.mobilehome.net/mobile-home-park-directory/new-hampshire/all',
'https://www.mobilehome.net/mobile-home-park-directory/vermont/all'
}
def get_names(link):
r = requests.get(link)
soup = BeautifulSoup(r.text,"lxml")
for items in soup.select("td[class='table-row-price']"):
name = items.select_one("h2 a").text
print(name)
if __name__ == '__main__':
for url in urls:
get_names(url)
Run Code Online (Sandbox Code Playgroud)
如果有一个像下面这样的分页的网址,我本可以设法做到这一切:
from bs4 import BeautifulSoup
import requests
page_no = 0
page_link = "https://www.mobilehome.net/mobile-home-park-directory/new-hampshire/all/page/{}"
while True:
page_no+=1
res = requests.get(page_link.format(page_no))
soup = BeautifulSoup(res.text,'lxml')
container = soup.select("td[class='table-row-price']")
if len(container)<=1:break
for content in container:
title = content.select_one("h2 a").text
print(title)
Run Code Online (Sandbox Code Playgroud)
但是,所有网址都没有分页.那么,无论是否有任何分页,我怎么能设法抓住所有这些?
我在python中编写了一个脚本来到达目标页面,其中每个类别在网站中都有可用的项目名称.我的下面的脚本可以从大多数链接获取产品名称(通过流动类别链接生成,然后是子类别链接).
该脚本可以解析在点击+
下面图像中可见的每个类别旁边的符号时显示的子类别链接,然后解析目标页面中的所有产品名称.这是此类目标页面之一.
如何从所有链接获取所有产品名称,无论其深度如何?
这是我到目前为止所尝试的:
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
link = "https://www.courts.com.sg/"
res = requests.get(link)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select(".nav-dropdown li a"):
if "#" in item.get("href"):continue #kick out invalid links
newlink = urljoin(link,item.get("href"))
req = requests.get(newlink)
sauce = BeautifulSoup(req.text,"lxml")
for elem in sauce.select(".product-item-info .product-item-link"):
print(elem.get_text(strip=True))
Run Code Online (Sandbox Code Playgroud)
如何找到trget链接:
我在php中编写了一个脚本,从网页上刮下一个可以看作头发掉落的标题.当我执行下面的脚本时,我收到以下错误:
注意:尝试在第16行的C:\ xampp\htdocs\runcode\testfile.php中获取非对象的属性"nodeValue".
我试过的脚本:
<?php
function get_content($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
$htmlContent = curl_exec($ch);
curl_close($ch);
return $htmlContent;
}
$link = "https://www.purplle.com/search?q=hair%20fall%20shamboo";
$xml = get_content($link);
$dom = @DOMDocument::loadHTML($xml);
$xpath = new DOMXPath($dom);
$title = $xpath->query('//h1[@class="br-hdng"]/span')->item(0)->nodeValue;
echo "{$title}";
?>
Run Code Online (Sandbox Code Playgroud)
我的预期输出是:
hair fall shamboo
Run Code Online (Sandbox Code Playgroud)
虽然xpath
我在上面的脚本中使用的似乎是正确的,但我在这里粘贴了title
可以找到的html元素的相关部分:
<h1 _ngcontent-c0="" class="br-hdng"><span _ngcontent-c0="" class="pr dib">hair fall shamboo<!----></span></h1>
Run Code Online (Sandbox Code Playgroud)
后记:在title
我要解析获取动态加载.由于我是php的新手,我不明白我尝试的方式是否准确.如果不是我应该做什么呢?
以下是我使用两种不同语言创建的脚本,发现它们像魔术一样工作.
我成功使用javascript
:
const puppeteer = require('puppeteer'); …
Run Code Online (Sandbox Code Playgroud) 我在python中编写了一个脚本,以便从javascript呈现的网页中获取最后一笔交易的价格.我可以得到内容如果我选择去selenium
.我的目标是不使用任何类似的浏览器模拟器,selenium
因为最新版本的Requests-HTML应该具有解析javascript加密内容的能力.但是,我无法顺利完成任务.当我运行脚本时,我收到以下错误.任何有关这方面的帮助将受到高度赞赏.
网站地址:webpage_link
我尝试过的脚本:
import requests_html
with requests_html.HTMLSession() as session:
r = session.get('https://www.gdax.com/trade/LTC-EUR')
js = r.html.render()
item = js.find('.MarketInfo_market-num_1lAXs',first=True).text
print(item)
Run Code Online (Sandbox Code Playgroud)
这是完整的追溯:
Exception in callback NavigatorWatcher.waitForNavigation.<locals>.watchdog_cb(<Task finishe...> result=None>) at C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py:49
handle: <Handle NavigatorWatcher.waitForNavigation.<locals>.watchdog_cb(<Task finishe...> result=None>) at C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py:49>
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py", line 145, in _run
self._callback(*self._args)
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py", line 52, in watchdog_cb
self._timeout)
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py", line 40, in _raise_error
raise error
concurrent.futures._base.TimeoutError: Navigation Timeout Exceeded: 3000 ms exceeded
Traceback (most recent call …
Run Code Online (Sandbox Code Playgroud) python web-scraping python-3.x python-requests python-requests-html
我已经在python中编写了一个脚本来使用函数从网站的登录页面中删除所有names
和links
相关的脚本.get_links()
.然后我创建了另一个功能.get_info()
来到达另一个页面(使用从第一个函数派生的链接),以便从那里刮取电话号码.
我根本不需要创建第二个功能如果我的目标是解析该网页中的两个项目,因为它们已经在着陆页中可用.
但是,我希望我的解析器表现的方式是names
在第二个函数中打印(从第一个函数开始)phone numbers
.最重要的是,我不想for loop
在第二个函数中踢出定义.如果for loop
不在第二个功能中那么问题就不会出现.没有使用for loop
我已经可以获得所需的输出.
到目前为止这是我的脚本:
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
url = "https://potguide.com/alaska/marijuana-dispensaries/"
def get_links(link):
session = requests.Session()
session.headers['User-Agent'] = 'Mozilla/5.0'
r = session.get(link)
soup = BeautifulSoup(r.text,"lxml")
for items in soup.select("#StateStores .basic-listing"):
name = items.select_one("h4 a").text
namelink = urljoin(link,items.select_one("h4 a").get("href")) ##making it a fully qualified url
get_info(session,name,namelink) ##passing session in order to reuse it …
Run Code Online (Sandbox Code Playgroud) 我在python中编写了一个脚本,以获取不同的链接,从而导致网页上的不同文章.在运行我的脚本后,我可以完美地获得它们.但是,我面临的问题是文章链接遍历多个页面,因为它们是大数字以适合单个页面.如果我点击下一页按钮,我可以在开发人员工具中看到附加的信息,这实际上是通过帖子请求产生一个ajax调用.由于下一页按钮没有附加链接,我找不到任何方法继续下一页并从那里解析链接.我试过用它post request
,formdata
但它似乎没有用.我哪里错了?
这是我点击下一页按钮时使用chrome dev工具获得的信息:
GENERAL
=======================================================
Request URL: https://www.ncbi.nlm.nih.gov/pubmed/
Request Method: POST
Status Code: 200 OK
Remote Address: 130.14.29.110:443
Referrer Policy: origin-when-cross-origin
RESPONSE HEADERS
=======================================================
Cache-Control: private
Connection: Keep-Alive
Content-Encoding: gzip
Content-Security-Policy: upgrade-insecure-requests
Content-Type: text/html; charset=UTF-8
Date: Fri, 29 Jun 2018 10:27:42 GMT
Keep-Alive: timeout=1, max=9
NCBI-PHID: 396E3400B36089610000000000C6005E.m_12.03.m_8
NCBI-SID: CE8C479DB3510951_0083SID
Referrer-Policy: origin-when-cross-origin
Server: Apache
Set-Cookie: ncbi_sid=CE8C479DB3510951_0083SID; domain=.nih.gov; path=/; expires=Sat, 29 Jun 2019 10:27:42 GMT
Set-Cookie: WebEnv=1Jqk9ZOlyZSMGjHikFxNDsJ_ObuK0OxHkidgMrx8vWy2g9zqu8wopb8_D9qXGsLJQ9mdylAaDMA_T-tvHJ40Sq_FODOo33__T-tAH%40CE8C479DB3510951_0083SID; domain=.nlm.nih.gov; path=/; expires=Fri, 29 Jun 2018 18:27:42 GMT
Strict-Transport-Security: …
Run Code Online (Sandbox Code Playgroud) 我写了一个脚本来解析craigslist中某些项目的名称和价格.在xpath
我的刮板内定义[我已正在努力的.事情是,当我尝试以通常的方式刮取物品然后应用try/except
块我可以避免IndexError
当某个价格的值为零时.我甚至尝试使用自定义功能使其工作并获得成功.
但是,在下面的代码片段中,我想应用lambda
函数来解决IndexError
错误.我试过但不能成功.
顺便说一句,当我运行代码它既不取任何东西也不抛出任何错误.
import requests
from lxml.html import fromstring
page = requests.get('http://bangalore.craigslist.co.in/search/rea?s=120').text
tree = fromstring(page)
# I wish to fix this function to make a go
get_val = lambda item,path:item.text if item.xpath(path) else ""
for item in tree.xpath('//li[@class="result-row"]'):
link = get_val(item,'.//a[contains(@class,"hdrlnk")]')
price = get_val(item,'.//span[@class="result-price"]')
print(link,price)
Run Code Online (Sandbox Code Playgroud) web-scraping ×10
python ×7
python-3.x ×7
vba ×2
curl ×1
domdocument ×1
excel ×1
excel-vba ×1
function ×1
lambda ×1
php ×1
selenium ×1