Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复SyntaxError:无法在元素上使用绝对路径_Python_Xpath - Fatal编程技术网

Python 如何修复SyntaxError:无法在元素上使用绝对路径

Python 如何修复SyntaxError:无法在元素上使用绝对路径,python,xpath,Python,Xpath,回溯: import requests from lxml import html SEARCH_URL = "https://www.yellowpages.com/search" def crawl(name, state, page=1): params={'search_terms': name, 'geo_location_terms': state, 'page': page} data = requests.get(SEARCH_URL, params=pa

回溯:

import requests
from lxml import html


SEARCH_URL = "https://www.yellowpages.com/search"


def crawl(name, state, page=1):
    params={'search_terms': name, 'geo_location_terms': state, 'page': page}
    data = requests.get(SEARCH_URL, params=params).text
    tree = html.fromstring(data)
    for items in tree.xpath("//div[@class='info']"):
        name = items.findtext(".//span[@itemprop='name']")
        address = items.findtext(".//span[@class='street-address']")
        phone = items.findtext(".//div[@itemprop='telephone']")
        showing = items.findtext("//*[@id='main-content']/div[2]/div[4]/p/text()")


        yield (name, address, phone, showing)


def search(name, state, pages=1):
    page = 1
    while page is not pages:
        for result in crawl(name, state, page=page):
            print result
        page +=1


if __name__ == '__main__':
    search('pizza', 'tx', pages=10)
回溯(最近一次呼叫最后一次):
文件“C:/Python27/Scripts/yellowpages.py”,第31行,在
搜索('pizza','tx',页面=10)
文件“C:/Python27/Scripts/yellowpages.py”,第25行,搜索中
对于爬网结果(名称、状态、页面=页面):
文件“C:/Python27/Scripts/yellowpages.py”,第16行,在爬网中
showing=items.findtext(“/*[@id='main-content']/div[2]/div[4]/p/text()”)
文件“src\lxml\lxml.etree.pyx”,第1550行,位于lxml.etree.\u Element.findtext(src\lxml\lxml.etree.c:59189)中
findtext中第320行的文件“C:\Python27\lib\site packages\lxml\\u elementpath.py”
el=查找(元素、路径、名称空间)
文件“C:\Python27\lib\site packages\lxml\\u elementpath.py”,第302行,在find中
it=iterfind(元素、路径、名称空间)
文件“C:\Python27\lib\site packages\lxml\\u elementpath.py”,第291行,在iterfind中
选择器=\构建\路径\迭代器(路径,名称空间)
文件“C:\Python27\lib\site packages\lxml\\u elementpath.py”,第260行,在构建路径迭代器中
raise SyntaxError(“无法在元素上使用绝对路径”)
SyntaxError:无法在元素上使用绝对路径

问题出在这一行:

Traceback (most recent call last):
  File "C:/Python27/Scripts/yellowpages.py", line 31, in <module>
    search('pizza', 'tx', pages=10)
  File "C:/Python27/Scripts/yellowpages.py", line 25, in search
    for result in crawl(name, state, page=page):
  File "C:/Python27/Scripts/yellowpages.py", line 16, in crawl
    showing = items.findtext("//*[@id='main-content']/div[2]/div[4]/p/text()")
  File "src\lxml\lxml.etree.pyx", line 1550, in lxml.etree._Element.findtext (src\lxml\lxml.etree.c:59189)
  File "C:\Python27\lib\site-packages\lxml\_elementpath.py", line 320, in findtext
    el = find(elem, path, namespaces)
  File "C:\Python27\lib\site-packages\lxml\_elementpath.py", line 302, in find
    it = iterfind(elem, path, namespaces)
  File "C:\Python27\lib\site-packages\lxml\_elementpath.py", line 291, in iterfind
    selector = _build_path_iterator(path, namespaces)
  File "C:\Python27\lib\site-packages\lxml\_elementpath.py", line 260, in _build_path_iterator
    raise SyntaxError("cannot use absolute path on element")
SyntaxError: cannot use absolute path on element
爬网功能更改为:

showing = items.findtext("//*[@id='main-content']/div[2]/div[4]/p/text()")
它将产生以下结果:

def crawl(name, state, page=1):
    params={'search_terms': name, 'geo_location_terms': state, 'page': page}
    data = requests.get(SEARCH_URL, params=params).text
    tree = html.fromstring(data)
    for items in tree.xpath("//div[@class='info']"):
        name = items.findtext(".//span[@itemprop='name']")
        address = items.findtext(".//span[@class='street-address']")
        phone = items.findtext(".//div[@itemprop='telephone']")
        showing = tree.xpath(".//div[@class='pagination']/p/text()")[0]

        yield (name, address, phone,showing)

您可以共享搜索URL吗?如果是语法错误,则与xpath无关。提供完整的错误回溯,以便有人能够提供帮助。
(None, None, None, '1-30\nof 3030')
('Port "A" Pizzeria', '407 E Avenue G', '(361) 749-5226', '1-30\nof 3030')
("Palio's Pizza Cafe", '3492 Legacy Dr', '(214) 308-6895', '1-30\nof 3030')
('Pizza Inn', '1501 Magnolia Ave', '(409) 242-2870', '1-30\nof 3030')
("Papa Murphy's Take & Bake Pizza", '815 SW Alsbury Blvd', '(817) 447-6777', '1-30\nof 3030')
("Lane's", '630 Sabine St', '(409) 787-3838', '1-30\nof 3030')
("Little Ceasar's Pizza", '1000 N Midkiff Rd', '(432) 694-3676', '1-30\nof 3030')
('The Gaff', '323 Beach Ave', '(361) 749-5970', '1-30\nof 3030')
("CiCi's Pizza", '1440 N Highway 77', '(972) 937-1222', '1-30\nof 3030')
......