python - 如何使列表理解具有“或”?
问题描述
我正在尝试从 Google 搜索中获取链接列表:
def google_word(word):
headers={'User-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763'}
url = 'https://google.com/search?q={}'.format(word)
res= requests.get(url, headers=headers)
tree= html.fromstring(res.text)
li = tree.xpath("//a[@href]") #list of links that conatin href
y = [link.get('href') for link in li if link.get('href').startswith("https://") if "google" not in link.get('href')]
现在,这段代码收集了以 " 开头的正确链接,https://"
我想做的也是添加"http://"
。我需要在列表理解中添加什么才能使其工作(我试图在一行中完成)?
解决方案
将元组添加到开头
y = [link.get('href') for link in li if link.get('href').startswith(("https://", "http://")) if "google" not in link.get('href')]
推荐阅读
- c - 嵌套在openmp中的lops
- jquery - 如何使用 ajax jquery 获取最终选择值?
- c - 在 C 中使用带有命令行参数的 system()
- html - 导航栏中的图标不会保持内联
- java - android studio中Firebase电话身份验证的空字符串错误
- python - 将烧瓶嵌入到带有装饰器的类中
- html - CSS:chrome-android中的全屏不尊重视口元
- java - javacc (ph-javacc-maven-plugin) 使用 case `\` 生成 java 开关
- ios - 如何在表格视图单元格的内容视图中居中 UILabel?
- java - Firebase 实时数据库:使用 AddChildEventListener 时如何获取 ChildrenCount?