Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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 AttributeError:type object';键入.re';没有属性';拆分';_Python_Split - Fatal编程技术网

Python AttributeError:type object';键入.re';没有属性';拆分';

Python AttributeError:type object';键入.re';没有属性';拆分';,python,split,Python,Split,我试图诈骗谷歌的URL链接。用户可以输入任何搜索,然后可以获取URL链接。但是这里的主要问题是这个分割函数不能工作。我修不好。所以请帮帮我 [[假设:任何用户都可以输入“所有无用的网站”,此时谷歌可以向我们显示结果。用户只能获取URL链接。]] from typing import re from bs4 import BeautifulSoup import requests user_input = input('Enter value for search : ') print('Pl

我试图诈骗谷歌的URL链接。用户可以输入任何搜索,然后可以获取URL链接。但是这里的主要问题是这个分割函数不能工作。我修不好。所以请帮帮我 [[假设:任何用户都可以输入“所有无用的网站”,此时谷歌可以向我们显示结果。用户只能获取URL链接。]]

from typing import re

from bs4 import BeautifulSoup
import requests

user_input = input('Enter value for search : ')
print('Please Wait')
page_source = requests.get("https://www.google.com/search?q=" + user_input)

soup = BeautifulSoup(page_source.text, 'html.parser')
print(soup.title)
print(soup.title.string)
print(soup.title.parent.name)

all_links = soup.find_all('a')

for link in all_links:
    link_google = re.split(":(?=http)", link["href"].replace("/url?q=", ""))
    print(link_google.find["a"])
更新以使代码正常工作:
  • 正确导入re
  • all\u links=soup.find\u all('a')
    all\u links=soup.find\u all('a',href=True)
  • 获取链接并像以前一样清理它(
    re.split()
    工作得很好,但它会返回一个列表),然后将该链接添加到列表中(解包列表)或打印它
  • 下面是为使其工作而更新的代码 一行列表的理解乐趣
    您从错误的位置导入了
    re
    。您需要通过
    导入re
    使用它,如下所示:

    import re
    ...
        link_google = re.split(":(?=http)", link["href"].replace("/url?q=", ""))
    
    

    我能和你谈谈吗?我需要更多的帮助,我需要你的帮助。如果您有空并看到我的评论,请通过邮件或skype联系。我的邮件地址是sakibhasan1420@gmail.comskype名称为“Sakib Hasan”或skype id中的相同邮件。只需发布更多有关堆栈溢出的问题,您就可以通过这种方式获得社区的支持!你能投票/接受我的答案吗?这样对别人会有帮助
    >>> {returns all the http links}
    
    google_links = [re.split(":(?=http)", link["href"].replace("/url?q=", ""))[0] for link in soup.find_all('a', href=True)]
    
    >>> {returns all the http links}
    
    import re
    ...
        link_google = re.split(":(?=http)", link["href"].replace("/url?q=", ""))