【问题标题】:Get word from an id in a web page in python从python网页中的id获取单词
【发布时间】:2016-01-30 15:10:32
【问题描述】:

我希望 python 从网站上读取特定的单词, 我在 urllib 和 bs4 中进行了搜索,但没有找到任何可以执行此操作的示例

我想要的示例:

在此页面上https://www.google.com.eg/search?q=weather%20in%20egypt&gws_rd=ssl

我想让 python 读取:

>>>id = wob_tm
>>>print 'weather is ' + id 
weather is 16

有可能吗?

【问题讨论】:

  • Google 真的不希望你这样做;你为什么不使用一些开放的天气 API 呢?
  • 这只是一个例子......
  • soup.select("#myid")
  • 成功了,非常感谢。

标签: python beautifulsoup urllib


【解决方案1】:

如果您想要此信息,您应该真正使用 API。但是,如果您只想要页面中的一条数据,并且您已经在变量中拥有 HTML 响应,那么使用简单的正则表达式(而不是解析整个页面)没有错:

id="wob_tm"[^>]*>([^<]+)

该值将在比赛的第 1 组中找到。
Demo here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-20
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多