简体   繁体   English

AttributeError:在更改数组期间“列表”对象没有属性“替换”

[英]AttributeError: 'list' object has no attribute 'replace' during change array

I'm don't have much experience with Python and I'm having a problem with making a web crawler. 我对Python没有太多经验,并且在制作网络搜寻器时遇到问题。 I'm trying to change a item in an array it gives this error 我正在尝试更改数组中的项目,它给出此错误

AttributeError: 'list' object has no attribute 'replace' AttributeError:“列表”对象没有属性“替换”

import urllib
import mechanize
import requests
import re
from bs4 import BeautifulSoup

def getGoogle(link):
        br = mechanize.Browser()
        br.set_handle_robots(False)
        br.addheaders=[('User-agent','chrome')]

        term = link.replace(" ", "+")
        query = "http://www.google.com/search?q="+term
        htmltext = br.open(query).read()
        soup2 = BeautifulSoup(htmltext, "html.parser")
        search = soup2.findAll('div', attrs={'id':'search'})
        searchtext = str(search[:])
        soup3 = BeautifulSoup(searchtext, "html.parser")
        list_items = soup3.findAll('li', attrs={'class', 'g'})
        for li in list_items:
                for h3 in li.findAll('h3'):
                        for a in h3.findAll('a'):
                                regex = "q(?!.*q).*?amp"
                                pattern = re.compile(regex)
                                source_url = re.findall(pattern, str(a))
                                results_array = [source_url]

                                for result in results_array:
                                        result.append(str(source_url[:].replace("q=","").replace("&amp", "")))

                                        for url in results_array:
                                                r = requests.get(url)

                                                soup = BeautifulSoup(r.content, "html.parser")

                                                g_data = soup.find_all(attrs={"name":"viewport"})

                                                for item in g_data:
                                                        return r.url, 'bevat een viewport'
                                                        break
                                                else:
                                                        return r.url, 'bevat GEEN viewport'

你可以这样做:

result.append(''.join(source_url).replace("q=","").replace("&amp", ""))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“列表”对象没有属性“替换”, - AttributeError: 'list' object has no attribute 'replace', “AttributeError:'list' object 没有属性'replace' - "AttributeError: 'list' object has no attribute 'replace' AttributeError:'list'对象没有属性'replace' - AttributeError: 'list' object has no attribute 'replace' AttributeError: 'list' object 没有属性 'replace' Python - AttributeError: 'list' object has no attribute 'replace' Python AttributeError: 'list' 对象没有属性 'replace' "fasttext" - AttributeError: 'list' object has no attribute 'replace' "fasttext" python AttributeError:“ NoneType”对象在列表中没有属性“ replace” - python AttributeError: 'NoneType' object has no attribute 'replace' in list AttributeError:'list'对象在尝试删除字符时没有属性'replace' - AttributeError: 'list' object has no attribute 'replace' when trying to remove character AttributeError:“ list”对象在尝试删除“ / n”时没有属性“ replace” - AttributeError: 'list' object has no attribute 'replace' while trying to remove '/n' AttributeError: 'list' object 没有属性 'replace' Selenium Python - AttributeError: 'list' object has no attribute 'replace' Selenium Python AttributeError: 'list' object 没有属性 - AttributeError: 'list' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM