简体   繁体   English

Python:在网站源代码中查找字符串时出现问题

[英]Python: Problems finding string in website source code

I open a website with urlopen. 我用urlopen打开一个网站。 I then put the website sourcecode into a variable like so 然后,我将网站源代码放入这样的变量中

source = website.read()

When I just print the source it comes out formatted correctly, however when I try to iterate through each line each character is it's own line. 当我只打印源代码时,它的格式正确,但是当我尝试遍历每行时,每个字符都是它自己的行。

for example 例如

when I just print it looks like this 当我刚打印时看起来像这样

<HTML> title</html>

When I do this 当我这样做时

for line in source:
      print line

it looks like this 看起来像这样

<
H
T
M
L
... etc

I need to find a string that starts with "var" and then print that entire line. 我需要找到一个以“ var”开头的字符串,然后打印整个行。

使用readlines()而不是read()获得行列表。

Or use: 或使用:

for line in source.split("\n"):
    ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM