Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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 请在lxml中帮助我_Python_Selenium_Web Scraping_Lxml - Fatal编程技术网

Python 请在lxml中帮助我

Python 请在lxml中帮助我,python,selenium,web-scraping,lxml,Python,Selenium,Web Scraping,Lxml,我在使用lxml时遇到了一些问题 我刚做了一个代码,工作正常,但我有两个问题 我想在同一行的名称和地址,每个条目应该在不同的行像 name1,adress1 name2,adress2 我不需要任何方括号中的数据 我必须输入500个代码,以便从外部文本/csv文件导入 请帮帮我怎么做 您不需要使用lxml,selenium提供了通过xpath查找元素 使用zip匹配姓名和地址 打开文本文件并迭代以获取行;使用str.strip获取代码 如果您想要的内容不受javascript的影响,您可

我在使用lxml时遇到了一些问题 我刚做了一个代码,工作正常,但我有两个问题

  • 我想在同一行的名称和地址,每个条目应该在不同的行像

    name1,adress1
    name2,adress2
    
  • 我不需要任何方括号中的数据

  • 我必须输入500个代码,以便从外部文本/csv文件导入 请帮帮我怎么做


  • 您不需要使用
    lxml
    selenium
    提供了
    通过xpath查找元素

    使用
    zip
    匹配姓名和地址

    打开文本文件并迭代以获取行;使用
    str.strip
    获取代码


    如果您想要的内容不受javascript的影响,您可以只使用
    lxml

    import lxml.html
    
    url = 'http://kmbsapps.konicaminolta.us/wheretobuy/main_search.jspx?productCategory=Office+Systems&sl_zip='
    
    with open('1.txt') as f:
        for line in f:
            cod = line.strip()
            tree = lxml.html.parse(url+cod)
            name = tree.xpath('//tr/td/span[@class="largecol"]/text()')
            address = tree.xpath('//tr/td/span[@class="smallcol"]/text()')
            name = [n for n in name if n.strip()]
            for n, a in zip(name, address):
                print(n, a)
    

    文本/csv是什么样子的?它在excell的专栏中我的意思是:有哪些专栏?csv文件类似于36116 36542 36693 35630 35802 35805 85719 85713 85040 85281 86301 72703很难说出评论中的内容。它们是一行一行的数字吗(没有标题)?我会在seprate中得到结果吗lines@user3891081是的,你会的。请自己试一试。我给了你工作代码。我仍然没有在单独的线路上获得数据这是结果3.1英里660大学大道H室伯明翰,阿拉巴马州35233 AMERI-TEK 2814林登大道伯明翰,阿拉巴马州35209 4.0英里1101格林伍德交叉法院贝塞默,阿拉巴马州35022 DEX成像,LLC 6205 AL Highway 69 Guntersville,AL 35976 9.8 miles 3240 Leeman Ferry Road Huntsville,AL 35801 KONICA MINOLTA BUSINESS SOLUTIONS U.S.,INC.位于阿拉巴马州斯科茨伯勒南宽街1523号35233@user3891081,你能把它发到别的地方(像pastebin这样的网站)吗?通过查看评论很难判断是否有新线。
    from selenium import webdriver
    
    browser = webdriver.Firefox()
    url = 'http://kmbsapps.konicaminolta.us/wheretobuy/main_search.jspx?productCategory=Office+Systems&sl_zip='
    
    with open('1.txt') as f:
        for line in f:
            cod = line.strip()
            browser.get(url+cod)
            name = browser.find_elements_by_xpath('//tr/td/span[@class="largecol"]')
            address = browser.find_elements_by_xpath('//tr/td/span[@class="smallcol"]')
            name = [n for n in name if n.text.strip()]  # Remove empty names
            for n, a in zip(name, address):
                print(n.text, a.text)
    
    import lxml.html
    
    url = 'http://kmbsapps.konicaminolta.us/wheretobuy/main_search.jspx?productCategory=Office+Systems&sl_zip='
    
    with open('1.txt') as f:
        for line in f:
            cod = line.strip()
            tree = lxml.html.parse(url+cod)
            name = tree.xpath('//tr/td/span[@class="largecol"]/text()')
            address = tree.xpath('//tr/td/span[@class="smallcol"]/text()')
            name = [n for n in name if n.strip()]
            for n, a in zip(name, address):
                print(n, a)